Qt在多线程中使用QTcpSocket碰见的问题
在Qt中写多线程网络通信时碰到了个问题,具体情形如下:
新建一个FileRecvThread继承QThread,重写run函数
class FileRecvThread : public QThread{ Q_OBJECTpublic: explicit FileRecvThread(QObject *parent = 0); FileRecvThread(quint32, QObject *parent = 0); ~FileRecvThread(); void initSignalSlots(); void setPort(quint32 port); quint32 getPort(); void dealShackHand(QByteArray *); void replyExpectShackHand();protected: void run();private: quint32 port; QTcpServer *pTcpServer; QTcpSocket *pTcpSocket;public slots: void startTcpServer(); void dealNewConnection(); void dealDatagram();FileRecvThread::FileRecvThread(quint32 port, QObject *parent) : QThread(parent){ this->port = port;}void FileRecvThread::startTcpServer(){ if (!this->pTcpServer) { this->pTcpServer = new QTcpServer; this->pTcpServer->listen(QHostAddress::Any, this->port); } else { this->pTcpServer->listen(QHostAddress::Any, this->port); }}void FileRecvThread::run(){ this->startTcpServer(); this->initSignalSlots(); this->exec();}void FileRecvThread::initSignalSlots(){ connect(this->pTcpServer, SIGNAL(newConnection()), this, SLOT(dealNewConnection()));}void FileRecvThread::dealNewConnection(){ this->pTcpSocket = this->pTcpServer->nextPendingConnection(); connect(this->pTcpSocket, SIGNAL(readyRead()), this, SLOT(dealDatagram()));}void FileRecvThread::dealDatagram(){ QByteArray byteArray = this->pTcpSocket->readAll(); //这里出现问题 [color=#FF0000]this->pTcpSocket->write(QByteArray("deal"));[/color]}