我正在尝试首次使用Qt创建多线程服务器。通常,人们将使用QTcpServer::nextPendingConnection()返回的套接字指针,并已插入其中的套接字句柄-但由于我要在单独的线程上与连接客户端进行接口(interface),因此我需要使用QTcpServer::incomingConnection( qintptr句柄)。经过一个非常沉闷,充满错误的调试 session 后,我设法将问题追根溯源到从未被解雇的qintptr handle

有没有人遇到过类似的问题-在最新版本的Qt上有什么变化吗?

这些是我尝试过的:

  • QTcpServer::incomingConnection()
  • QTcpServer::incomingConnection(qintptr handle)
  • QTcpServer::incomingConnection(qintptr socketDescriptor)

  • 编辑:

    创建服务器实例:
    TestServer *myServer = new TestServer();
    myServer->initializeServer(1234);
    

    哪个调用:
    void TestServer::initializeServer(quint16 port)
    {
    
    mainServer = new QTcpServer(this);
    
    mainServer->listen(QHostAddress::Any, port);
    
    qDebug() << "Listening for connections on port: " << port;
    
    
    
    }
    

    服务器正在监听。当客户端连接入局时,应该调用incomingConnection(qintptr handle):
    void TestServer::incomingConnection(qintptr socketDescriptor){
    
    TestClient *client = new TestClient(this);
    client->setSocket(socketDescriptor);
    
    }
    

    哪个调用:
    void TestClient::setSocket(quint16 socketDescr)
    {
    socket = new QTcpSocket(this);
    
    socket->setSocketDescriptor(socketDescr);
    
    connect(socket, SIGNAL(connected()),this,SLOT(connected()));
    connect(socket, SIGNAL(disconnected()),this,SLOT(disconnected()));
    connect(socket, SIGNAL(readyRead()),this,SLOT(readyRead()));
    
    
    
    }
    

    调用connect()信号:
    void TestClient::connected()
    {
    qDebug() << "Client connected..."; // This debug never appears in the console, since QTcpServer::incomingConnection isn't being fired.
    
    }
    

    最佳答案

    您的代码有一些错误:

  • TestServer上,您的QTcpServer可能已聚合,但是您需要继承它。在这种情况下,您尝试覆盖incomingConnection()方法,但是您没有基类,而只是创建新的incomingConnection(),而不是覆盖。
  • 您可以从qintptr descriptor获取incomingConnection()变量,但可以通过quint16方法设置setSocket()类型。
  • 您可能将服务器的客户端和您的部件的客户端混合在一起,它们只是获得传入的连接并处理套接字数据。

  • 我在下面写了一些小例子,以帮助您了解tcp客户端与服务器之间的通信。

    服务器部分

    主要部分是服务器本身:
    #include <QTcpServer>
    
    class TestServer: public QTcpServer
    {
    public:
        TestServer(QObject *parent = 0);
        void incomingConnection(qintptr handle) Q_DECL_OVERRIDE;
    };
    

    只是看一下:我不讨厌 QTcpServer ,但是继承了它。在这种情况下,您可以正确覆盖 incomingConnection() 方法。
    在构造函数中,我们只是使用 listen() 方法启动服务器以进行监听:
    TestServer::TestServer(QObject *parent):
        QTcpServer(parent)
    {
        if (this->listen(QHostAddress::Any, 2323)) {
            qDebug() << "Server start at port: " << this->serverPort();
        } else {
            qDebug() << "Start failure";
        }
    }
    

    然后是覆盖incomingConnection()的时间:
    void TestServer::incomingConnection(qintptr handle)
    {
        qDebug() << Q_FUNC_INFO << " new connection";
        SocketThread *socket = new SocketThread(handle);
    
        connect(socket, SIGNAL(finished()), socket, SLOT(deleteLater()));
        socket->start();
    }
    

    我创建SocketThread对象来处理传入的数据:
    #include <QThread>
    #include <QObject>
    
    class QTcpSocket;
    
    class SocketThread: public QThread
    {
        Q_OBJECT
    public:
        SocketThread(qintptr descriptor, QObject *parent = 0);
        ~SocketThread();
    
    protected:
        void run() Q_DECL_OVERRIDE;
    
    private slots:
        void onConnected();
        void onReadyRead();
        void onDisconnected();
    
    private:
        QTcpSocket *m_socket;
        qintptr m_descriptor;
    };
    

    我们从 QThread 继承来使我们的服务器成为多线程,因此我们必须重写 run() 方法:
    SocketThread::SocketThread(qintptr descriptor, QObject *parent)
        :  QThread(parent), m_descriptor(descriptor)
    {
    
    }
    
    void SocketThread::run()
    {
        qDebug() << Q_FUNC_INFO;
        m_socket = new QTcpSocket;
        m_socket->setSocketDescriptor(m_descriptor);
    
        connect(m_socket, SIGNAL(readyRead()),    this, SLOT(onReadyRead()), Qt::DirectConnection);
        connect(m_socket, SIGNAL(disconnected()), this, SLOT(onDisconnected()), Qt::DirectConnection);
    
        exec();
    }
    

    这样,我们可以初始化 QTcpSocket ,设置套接字描述符,将其与 readyRead() disconnected() 信号连接并启动事件循环。
    void SocketThread::onReadyRead()
    {
        QDataStream in(m_socket);
        in.setVersion(QDataStream::Qt_5_5);
    
        QString message;
        in >> message;
        qDebug() << message;
    
        m_socket->disconnectFromHost();
    }
    
    void SocketThread::onDisconnected()
    {
        m_socket->close();
        // Exit event loop
        quit();
    }
    

    onReadyRead()处,只需从客户端读取一些QString,将其写入控制台并与主机断开连接即可。在onDisconnected()处,我们在close套接字连接并退出事件循环。

    客户部分

    这只是示例和不好的样式,但是我在MainWindow信号上的QPushButton::clicked类上创建了到服务器的连接:
    void MainWindow::on_pushButton_clicked()
    {
        QTcpSocket *client = new QTcpSocket;
        connect(client, SIGNAL(connected()), this, SLOT(connected()));
        connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
    
        client->connectToHost(QHostAddress::LocalHost, 2323);
        client->waitForConnected();
    
        if (client->state() != QAbstractSocket::ConnectedState ) {
            qDebug() << Q_FUNC_INFO << " can't connect to host";
            delete client;
            return;
        }
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_5_5);
        out << QString("Hello");
        out.device()->seek(0);
        client->write(block);
    }
    
    void MainWindow::connected()
    {
        qDebug() << Q_FUNC_INFO << " client connected";
    }
    

    我创建新的QTcpSocket,将其连接到信号,然后尝试连接到主机(在我的情况下是localhost)。等待连接并检查 socket 状态。如果一切正确,我写套接字QString-仅作为示例。

    这是组织多线程客户端-服务器体系结构的一种可能方法,我希望它对您有所帮助,并且您会发现自己的错误。

    09-06 04:18