Qt之QTcpServer/QTcpSocket简单收发信息(1)

来源:互联网 发布:软件开发行业前景 编辑:程序博客网 时间:2024/05/17 22:08

转自:http://blog.csdn.net/wyz365889/article/details/7070284

用QT包装好的东西做socket类东西,我只能说啥这么简单呢。

waitForConnected() 等待链接的建立
waitForReadyRead() 等待新数据的到来
waitForBytesWritten() 等待数据写入socket
waitForDisconnected() 等待链接断开

开始前在项目.pro里面添加如下:

       QT += core guinetwork

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

[cpp] view plaincopyprint?
  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.   
  10.   
  11.     this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));  
  12.     this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));  
  13. }  
  14.   
  15. Testnet::~Testnet()  
  16. {  
  17.     delete ui;  
  18. }  
  19.   
  20. void Testnet::startTcpserver()  
  21. {  
  22.     m_tcpServer = new QTcpServer(this);  
  23.   
  24.     m_tcpServer->listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip  
  25.   
  26.     connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。  
  27. }  
  28.   
  29. void Testnet::newConnect()  
  30. {  
  31.         m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket  
  32.         connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽  
  33.   
  34. }  
  35.   
  36. void Testnet::readMessage() //读取信息  
  37. {  
  38. //    ui->textEdit_rec->te   
  39.     QByteArray qba= m_tcpSocket->readAll(); //读取  
  40.     qDebug()<<qba;  
  41.     QString ss=QVariant(qba).toString();  
  42.     ui->textEdit_rec->setText(ss);  
  43.   
  44. }  
  45.   
  46. void Testnet::sendMessage() //发送信息  
  47. {  
  48.     QString strMesg= ui->lineEdit_sendmessage->text();  
  49.     qDebug()<<strMesg;  
  50.     m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送  
  51. }  


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

[cpp] view plaincopyprint?
  1. #include "testnet_c.h"   
  2. #include "ui_testnet_c.h"   
  3.   
  4.   
  5.   
  6. testnet_c::testnet_c(QWidget *parent) :  
  7.     QMainWindow(parent),  
  8.     ui(new Ui::testnet_c)  
  9. {  
  10.     ui->setupUi(this);  
  11.   
  12.     this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));  
  13.     this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));  
  14. }  
  15.   
  16. testnet_c::~testnet_c()  
  17. {  
  18.     delete ui;  
  19. }  
  20.   
  21.   
  22. void testnet_c::connectServer()  
  23. {  
  24.     m_tcpSocket = new QTcpSocket(this);  
  25.     m_tcpSocket->abort();  
  26.     m_tcpSocket->connectToHost("192.168.1.178",19999);  
  27.   
  28.     connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));  
  29. }  
  30.   
  31. void testnet_c::readMesg() //读取信息  
  32. {  
  33.    QByteArray qba =   m_tcpSocket->readAll();  
  34.   
  35.    ui->textEdit_recmesg->clear();  
  36.   
  37.    qDebug()<<qba;  
  38.    QString ss=QVariant(qba).toString();  
  39.    ui->textEdit_recmesg->setText(ss);  
  40. }  
  41.   
  42. void testnet_c::sendMesg() //发送信息  
  43. {  
  44.     QString ss= ui->lineEdit_sendmesg->text();  
  45.     m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));  
  46.     ui->lineEdit_sendmesg->clear();  
  47. }  


 

实验结果如下: