1. 程式人生 > >Qt之QTcpServer/QTcpSocket簡單收發資訊(1)

Qt之QTcpServer/QTcpSocket簡單收發資訊(1)

用QT包裝好的東西做socket類東西,我只能說啥這麼簡單呢。

waitForConnected() 等待連結的建立
waitForReadyRead() 等待新資料的到來
waitForBytesWritten() 等待資料寫入socket
waitForDisconnected() 等待連結斷開

開始前在專案.pro裡面新增如下:

       QT +=coreguinetwork

=========server==============


  1. #include "testnet.h"
  2. #include "ui_testnet.h"
  3. #include <QtGui>
  4. Testnet::Testnet(QWidget *parent) :  
  5.     QMainWindow(parent),  
  6.     ui(new Ui::Testnet)  
  7. {  
  8.     ui->setupUi(this);  
  9.     this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));  
  10.     this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));  
  11. }  
  12. Testnet::~Testnet()  
  13. {  
  14.     delete
     ui;  
  15. }  
  16. void Testnet::startTcpserver()  
  17. {  
  18.     m_tcpServer = new QTcpServer(this);  
  19.     m_tcpServer->listen(QHostAddress::Any,19999); //監聽任何連上19999埠的ip
  20.     connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新連線訊號觸發,呼叫newConnect()槽函式,這個跟訊號函式一樣,其實你可以隨便取。
  21. }  
  22. void Testnet::newConnect()  
  23. {  
  24.         m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每個連進來的socket
  25.         connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可讀的資訊,觸發讀函式槽
  26. }  
  27. void Testnet::readMessage() //讀取資訊
  28. {  
  29. //    ui->textEdit_rec->te
  30.     QByteArray qba= m_tcpSocket->readAll(); //讀取
  31.     qDebug()<<qba;  
  32.     QString ss=QVariant(qba).toString();  
  33.     ui->textEdit_rec->setText(ss);  
  34. }  
  35. void Testnet::sendMessage() //傳送資訊
  36. {  
  37.     QString strMesg= ui->lineEdit_sendmessage->text();  
  38.     qDebug()<<strMesg;  
  39.     m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //傳送

=======client========

  1. #include "testnet_c.h"
  2. #include "ui_testnet_c.h"
  3. testnet_c::testnet_c(QWidget *parent) :  
  4.     QMainWindow(parent),  
  5.     ui(new Ui::testnet_c)  
  6. {  
  7.     ui->setupUi(this);  
  8.     this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));  
  9.     this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));  
  10. }  
  11. testnet_c::~testnet_c()  
  12. {  
  13.     delete ui;  
  14. }  
  15. void testnet_c::connectServer()  
  16. {  
  17.     m_tcpSocket = new QTcpSocket(this);  
  18.     m_tcpSocket->abort();  
  19.     m_tcpSocket->connectToHost("192.168.1.178",19999);  
  20.     connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));  
  21. }  
  22. void testnet_c::readMesg() //讀取資訊
  23. {  
  24.    QByteArray qba =   m_tcpSocket->readAll();  
  25.    ui->textEdit_recmesg->clear();  
  26.    qDebug()<<qba;  
  27.    QString ss=QVariant(qba).toString();  
  28.    ui->textEdit_recmesg->setText(ss);  
  29. }  
  30. void testnet_c::sendMesg() //傳送資訊
  31. {  
  32.     QString ss= ui->lineEdit_sendmesg->text();  
  33.     m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));  
  34.     ui->lineEdit_sendmesg->clear();