qt中socket通信流程图_在Qt的多线程网络通信中一个TCPSocket怎么实现读和写?

该博客介绍了在Qt环境中如何使用多线程实现TCP Socket通信,包括服务器端的socket设置、数据流处理以及信号槽连接。在`run`函数中,通过`QTcpSocket`的`setSocketDescriptor`方法建立连接,并监听`readyRead`和`stateChanged`信号。在`UserLogin`函数中,实现了数据的读取和验证。然而,遇到的问题是,虽然初次通信成功,但后续客户端的发送无法得到响应。作者寻求解决客户端读写在同一线程中实现的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键部分代码这是服务器的代码,对run函数的重新实现;voidTcpThread::run(){tcpSocket=newQTcpSocket;//将Server传来的socketfd与刚创建的tcpSocket关联if(!tcpSocket->setSocketDes...

关键部分代码这是服务器的代码,对run函数的重新实现;void TcpThread::run(){ tcpSocket = new QTcpSocket; //将Server传来的socketfd与刚创建的tcpSocket关联 if(!tcpSocket->setSocketDescriptor(socketfd)) { emit error(tcpSocket->error()); return ; } in.setVersion(QDataStream::Qt_5_1); in.setDevice(tcpSocket); connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receiveMessage()),Qt::BlockingQueuedConnection); connect(tcpSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(statechanged()),Qt::BlockingQueuedConnection); exec();}登陆函数void TcpThread::UserLogin(){ QString account,passwd; //接收 in>>account>>passwd;//QDataStream in FindUser(account.toUInt(),0,&user); if(user.PASSWD == passwd) { qDebug()<write(outblock); outblock.resize(0); }}当第一次调用的时候就出现警告:QObject: Cannot create children for a parent that is in a different thread.(Parent is QNativeSocketEngine(0x1aa34cc8), parent's thread is TcpThread(0x1aa3a328), current thread is QThread(0x3d4ae8),但是服务器和客户端还是能够收到彼此发过去的数据,但是客户端再一次发送就不能响应了;我想通过一个线程实现客户端的读和写!该怎么改进???

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值