Qt - 在一个新线程中处理QTcpSocket

CDT*_*CDT 6 c++ qt

尝试在全局线程池的新线程中处理连接的客户端套接字:

m_threadPool = QThreadPool::globalInstance();

void TCPListenerThread::onNewConnection()
{
    QTcpSocket *clientSocket = m_tcpServer->nextPendingConnection();
    clientSocket->localPort();
    m_connectThread = new TCPConnectThread(clientSocket);
    m_threadPool->start(m_connectThread);
}
Run Code Online (Sandbox Code Playgroud)

这是TCPConnectThread:

class TCPConnectThread : public QRunnable {
    TCPConnectThread::TCPConnectThread(QTcpSocket *_socket)
    {
        m_socket = _socket;
        this->setAutoDelete(false);
    }


    void TCPConnectThread::run()
    {
        if (! m_socket->waitForConnected(-1) )
            qDebug("Failed to connect to client");
        else
            qDebug("Connected to %s:%d %s:%d", m_socket->localAddress(), m_socket->localPort(), m_socket->peerAddress(), m_socket->peerPort());

        if (! m_socket->waitForReadyRead(-1))
            qDebug("Failed to receive message from client") ;
        else
            qDebug("Read from client: %s",   QString(m_socket->readAll()).toStdString().c_str());

        if (! m_socket->waitForDisconnected(-1))
            qDebug("Failed to receive disconnect message from client");
        else
            qDebug("Disconnected from client");
    }
}
Run Code Online (Sandbox Code Playgroud)

我一直在犯这些错误.似乎跨线程QTcpSocket处理是不可行的(参见Michael的回答).

一些错误:

QSocketNotifier: socket notifiers cannot be disabled from another thread  

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events t objects owned by a different thread.  
Run Code Online (Sandbox Code Playgroud)

我应该处理QTcpSocket不同的线程吗?
如果我想QTcpSocket在不同的线程中处理,我该怎么办?
或者有没有办法QTcpSocket从文件描述符创建?

huy*_*itw 8

我认为这个页面有你的答案:

如果要在另一个线程中将传入连接作为新的QTcpSocket对象处理,则必须将socketDescriptor传递给另一个线程并在那里创建QTcpSocket对象并使用其setSocketDescriptor()方法.

为此,您必须继承QTcpServer并覆盖虚方法incomingConnection.

在该方法中,创建子线程,该子线程将为QTcpSocket子套接字创建一个新线程.

例如:

class MyTcpServer : public QTcpServer
{
protected:
    virtual void incomingConnection(int socketDescriptor)
    {
         TCPConnectThread* clientThread = new TCPConnectThread(socketDescriptor);
         // add some more code to keep track of running clientThread instances...
         m_threadPool->start(clientThread);
    }
};

class TCPConnectThread : public QRunnable
{
private:    
    int m_socketDescriptor;
    QScopedPointer<QTcpSocket> m_socket;

public:
    TCPConnectionThread(int socketDescriptor)
        : m_socketDescriptor(socketDescriptor)
    {
        setAutoDelete(false);
    }

protected:    
    void TCPConnectThread::run()
    {
        m_socket.reset(new QTcpSocket());
        m_socket->setSocketDescriptor(m_socketDescriptor);

        // use m_socket
    }
};
Run Code Online (Sandbox Code Playgroud)

或尝试moveToThread()在套接字上使用.