【IT专家】Qt 中关于socket的读与写

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

本文由我司收集整编,推荐下载,如有疑问,请与我司联系

Qt 中关于socket的读与写

2012/10/22 4042 ui- setupUi(this); this- connect(ui- pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver())); this- connect(ui- pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage())); Testnet::~Testnet() delete ui; void Testnet::startTcpserver() m_tcpServer = new QTcpServer(this); m_tcpServer- listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。void Testnet::newConnect() m_tcpSocket = m_tcpServer- nextPendingConnection(); //得到每个连进来的socket

connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽void Testnet::readMessage() //读取信息QByteArray qba= m_tcpSocket- readAll(); //读取qDebug() qba; QString ss=QVariant(qba).toString(); ui- textEdit_rec- setText(ss); void Testnet::sendMessage() //发送信息QString strMesg= ui- lineEdit_sendmessage- text(); qDebug() strMesg; m_tcpSocket- write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送客户端:// client #include “testnet_c.h” #include “ui_testnet_c.h” testnet_c::testnet_c(QWidget *parent) : QMainWindow(parent), ui(new Ui::testnet_c) ui- setupUi(this); this- connect(ui- pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer())); this- connect(ui- pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg())); testnet_c::~testnet_c() delete ui; void testnet_c::connectServer() m_tcpSocket = new QTcpSocket(this); m_tcpSocket- abort(); m_tcpSocket- connectToHost(“192.168.1.178”,19999); connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg())); void testnet_c::readMesg() //读取信息QByteArray qba = m_tcpSocket- readAll(); ui- textEdit_recmesg- clear(); qDebug() qba; QString ss=QVariant(qba).toString(); ui-

相关文档
最新文档