C++ (Qt)#ifndef QSERVERTHREAD_H#define QSERVERTHREAD_H#define NUMTOSTR(x) QString::number(x) #include <QThread>#include <QTcpSocket> class QServerThread : public QThread{ Q_OBJECT public: explicit QServerThread(int socketDescriptor, QObject *parent); ~QServerThread(); void run(); private: int socketDescriptor; qint16 m_nNextBlockSize; QTcpSocket pClientSocket; QThread *parentThread; void sendToClient(const QString &nstr); private slots: void slotReadFromClient(); void slotDisconnect(); signals: void error(QTcpSocket::SocketError socketError); void print(const QString &info);}; #endif // QSERVERTHREAD_H //=============================== #define PRINT(x) emit QServerThread::print((QString)x); QServerThread::QServerThread(int socketDescriptor, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor), parentThread(parent->thread()){ this->moveToThread(this);} QServerThread::~QServerThread(){ delete parentThread;} void QServerThread::run(){ if (!pClientSocket.setSocketDescriptor(socketDescriptor)) { emit error(pClientSocket.error()); return; } //pClientSocket.moveToThread(this); PRINT("New connection from "+ pClientSocket.peerAddress().toString() + ":" + NUMTOSTR(pClientSocket.peerPort())) sendToClient("HELLO WORLD"); connect(&pClientSocket, SIGNAL(readyRead()), this, SLOT(slotReadFromClient())); connect(&pClientSocket, SIGNAL(disconnected()), this, SLOT(slotDisconnect())); connect(&pClientSocket, SIGNAL(destroyed()), this, SLOT(quit())); exec(); this->moveToThread(parentThread);}
C++ (Qt)class QServerBase : public QTcpServer{ Q_OBJECTpublic: explicit QServerBase(QObject *parent = 0); protected: void incomingConnection(int socketDescriptor); signals: void log(const QString &info); }; //======================== QServerBase::QServerBase(QObject *parent) : QTcpServer(parent){} void QServerBase::incomingConnection(int socketDescriptor){ QServerThread *thread = new QServerThread(socketDescriptor, this); //thread->moveToThread(thread); in constructor connect(thread, SIGNAL(print(QString)), this, SIGNAL(log(QString))); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start();}
C++ (Qt)QObject::moveToThread: Cannot move objects with a parent // on connect clientQObject: Cannot create children for a parent that is in a different thread.(Parent is QTcpSocket(0x1412000), parents thread is QThread(0x627530), current thread is QServerThread(0x1411ff0)Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly // on disconnect client
C++ (Qt)QServerThread::~QServerThread(){ delete parentThread;}
C++ (Qt)class QServerThread : public QThread{ Q_OBJECT public: explicit QServerThread(int socketDescriptor, QObject *parent); void run(); private: int socketDescriptor; qint16 m_nNextBlockSize; //QTcpSocket *pClientSocket; void sendToClient(QTcpSocket *pClientSocket, const QString &nstr); private slots: void slotReadFromClient(); void slotDisconnect(); signals: void error(QTcpSocket::SocketError socketError); void print(const QString &info);};//====================================QServerThread::QServerThread(int socketDescriptor, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor){ this->moveToThread(this); // this line doesn`t work.} void QServerThread::run(){ QTcpSocket *pClientSocket = new QTcpSocket(this); if (!pClientSocket->setSocketDescriptor(socketDescriptor)) { emit error(pClientSocket->error()); return; } PRINT("New connection from "+ pClientSocket->peerAddress().toString() + ":" + NUMTOSTR(pClientSocket->peerPort())) sendToClient(pClientSocket,"HELLO WORLD"); connect(pClientSocket, SIGNAL(readyRead()), this, SLOT(slotReadFromClient())); connect(pClientSocket, SIGNAL(disconnected()), this, SLOT(slotDisconnect())); connect(pClientSocket, SIGNAL(destroyed()), this, SLOT(quit())); exec();} void QServerThread::sendToClient(QTcpSocket *pClientSocket, const QString &nstr){ QByteArray dataBlock; QDataStream out(&dataBlock, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << qint16(0) << QTime::currentTime() << nstr; out.device()->seek(0); out << qint16(dataBlock.size() - sizeof(qint16)); pClientSocket->write(dataBlock); PRINT("Sending data to "+ pClientSocket->peerAddress().toString() + ":" + NUMTOSTR(pClientSocket->peerPort()))} void QServerThread::slotReadFromClient(){ QTcpSocket *pClientSocket = (QTcpSocket*)sender(); QDataStream in(pClientSocket); in.setVersion(QDataStream::Qt_4_0); for(;;){ if(!m_nNextBlockSize){ if (pClientSocket->bytesAvailable() < (int)sizeof(qint16)){ break; } in >> m_nNextBlockSize; } if (pClientSocket->bytesAvailable() < m_nNextBlockSize){ break; } QString data; QTime time; in >> time >> data; PRINT(pClientSocket->peerAddress().toString() + ":" + NUMTOSTR(pClientSocket->peerPort())+" has send " + NUMTOSTR(m_nNextBlockSize)+"b > " + data) m_nNextBlockSize = 0; sendToClient(pClientSocket, "Server Response > Received \"" + data + "\""); }} void QServerThread::slotDisconnect(){ QTcpSocket *pClientSocket = (QTcpSocket*)sender(); PRINT(pClientSocket->peerAddress().toString() + ":" + NUMTOSTR(pClientSocket->peerPort())+" is disconnected") pClientSocket->deleteLater();}
C++ (Qt)QObject::moveToThread: Cannot move objects with a parent // on new connectionQObject: Cannot create children for a parent that is in a different thread.(Parent is QServerThread(0x6dc038), parents thread is QThread(0x817530), current thread is QServerThread(0x6dc038)Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly // on close
C++ (Qt) QTcpSocket *pClientSocket = new QTcpSocket(this);
C++ (Qt) QTcpSocket *pClientSocket = new QTcpSocket();
C++ (Qt)QTcpSocket *pClientSocket = new QTcpSocket();
C++ (Qt)m_pServerBase = new QServerBase(this);
C++ (Qt)void QServerThread::slotDisconnect(){ QTcpSocket *pClientSocket = (QTcpSocket*)sender(); //some code pClientSocket->deleteLater();}
C++ (Qt)this->moveToThread(this->thread());
C++ (Qt)QServerBase::QServerBase() 0x145c QServerThread::QServerThread() 0x145c QServerThread::QServerThread(QObject *parent) 0x145c // parent thread idQThread::run() 0x1504 QServerThread::slotReadFromClient() 0x145c QServerThread::slotReadFromClient() 0x145c