在Qt的多线程网络通信中一个TCPSocket怎么实现读和写? 5
关键部分代码这是服务器的代码,对run函数的重新实现;voidTcpThread::run(){tcpSocket=newQTcpSocket;//将Server传来的s...
关键部分代码这是服务器的代码,对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()<<"login success"; //发送 QByteArray outblock; QDataStream out(&outblock,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_1); out<<int(0x0001)<<int(0); tcpSocket->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),但是服务器和客户端还是能够收到彼此发过去的数据,但是客户端再一次发送就不能响应了;我想通过一个线程实现客户端的读和写!该怎么改进???
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询