用QTcpSocket实现的客户端.
要求:
1,有一个线程不停的等待接收这个socket的数据(有数据一到,就接收,然后写入一个全局变量(QString)中).
2, 要求主线程,可以对这个socket发送数据.
我自己写,老是出现socket notifiers cannot be enabled from another thread.
class SocketThread : public QThread { Q_OBJECT public: explicit SocketThread(QObject *parent = 0): QThread(parent) {} public: void run() 毕业论文 { m_pClient = new QTcpSocket(); m_pClient->connectToHost("127.0.0.1", 8866); if (m_pClient->waitForConnected(5000)) { qDebug() << "sokcet thread connected"; //sendData("GetMember"); } QThread::exec(); } public slots: void recvData() { QByteArray arr = m_pClient->readAll(); QString recvStr(arr); qDebug() << "socket thread::recvData: " << recvStr; gRecvDataStr.append(recvStr); } void sendData(QString sendStr) { m_pClient->write(sendStr.toStdString().c_str()); } public: QTcpSocket *m_pClient; };
//调用 SocketThread *p = new SocketThread(); p->start(); Sleep(300); p->sendData("GetMember");
试下 movetothread