class MLNetThread : public QThread{ Q_OBJECTpublic: MLNetThread(int socketDesc, QObject *pParent = 0); void run();signals: void processData(QByteArray *data);private: int socketDescriptor; QByteArray *data; QTcpSocket *socket;private slots: void readSocket();};
MLNetThread::MLNetThread(int socketDesc, QObject* pParent): QThread(pParent){ socketDescriptor = socketDesc;}void MLNetThread::run(){ data = new QByteArray(); socket = new QTcpSocket(); connect(socket, SIGNAL(readyRead()), this , SLOT(readSocket())); socket->setSocketDescriptor(socketDescriptor); exec();}void MLNetThread::readSocket(){ data->append(socket->readAll()); emit processData(data);}
C++ (Qt)void MLNetThread::run(){ QTcpSocket socket; connect(&socket, SIGNAL(readyRead()), this , SLOT(readSocket())); socket.setSocketDescriptor(socketDescriptor); exec();}