Writing data to a TcpSocket in own Thread

前端 未结 1 1638
忘掉有多难
忘掉有多难 2021-01-03 06:41

I got a problem with my threaded TCP-Server.
I can open my Server, a new Socket is created an I can receive data over the socket (I used the readyRead() signal and then

相关标签:
1条回答
  • 2021-01-03 07:03

    Your error message means:

    Your signal-slot communication happens on ServerThread instance which was created and belongs to main thread. However instance of ServerThread member m_socket was created and belongs to other thread.

    I would suggest you to create simple QThread and move your object into it.

    void Server::incomingConnection(int socketDescriptor) {
      QThread *t = new QThread();
      ClientConnection *client = new ClientConnection(socketDescriptor);
      client->moveToThread(t);
    
      // go
      t->start();
    }
    
    class ClientConnection : public QObject {
    Q_OBJECT
    public:
      ClientConnection(int socketDescriptor, QObject *parent = 0);
    public slots:
      void writeData(QString data);
    private:
      int m_socketDescriptor;
      QTcpSocket *m_socket;
    }
    
    0 讨论(0)
提交回复
热议问题