QtNetwork:为什么我检测不到传入的连接?(incomingConnection() 永远不会被触发)

QtNetwork: Why don't I detect incomming connections ? (incomingConnection() is never fired)

本文关键字:永远 incomingConnection 为什么 检测 QtNetwork 连接      更新时间:2023-10-16

我坚持学习关于线程qt网络的教程(在这里:http://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html),我做了一些小改动,并将其集成到我的主程序中。然而,incomingConnection()从未被执行,另一方面,客户端能够连接。由于我想使用incomingConnection(),所以使用SIGNAL(newConnection())已经过时了,但即使这样也不起作用。

有人知道出了什么问题吗?

这是我的.h

#include <QtNetwork>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>
class WirelessNetThread: public Thread
{
    Q_OBJECT
public:
    WirelessNetThread(int socketDescriptor, QObject * parent);
    void run() Q_DECL_OVERRIDE;
signals:
    void error(QTcpSocket::SocketError socketError);
private:
    int socketDescriptor;
    QString text;
};
class WirelessNet : public QTcpServer
{
    Q_OBJECT
public:
    WirelessNet(QObject *parent = 0);
protected:
    void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
};

.cpp

WirelessNetThread::WirelessNetThread(int socketDescriptor, QObject *parent):QThread(parent), socketDescriptor(socketDescriptor)
{
}
void WirelessNetThread::run()
{
    QTcpSocket tcpSocket;
    if ( !tcpSocket.setSocketDescriptor(socketDescriptor))
    {
        emit error(tcpSocket.error());
        return;
    }
    tcpSocket.disconnectFromHost();
    tcpSocket.waitForDisconnected();
}
WirelessNet::WirelessNet(QObject *parent): QTcpServer(0)
{
    listen(QHostAddress::Any, 5220);
    printf("is listening %dn", this->isListening());
}
void WirelessNet::incomingConnection(qintptr socketDescriptor)
{
    qDebug() << "incomming n";
    printf("incomming n");
    WirelessNetThread *thread = new WirelessNetThread(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}

这里是我的主程序的摘录,它是在哪里启动的(顺便说一句,如果我省略moveToThread():也没关系

WirelessNet *wifi = new WirelessNet(this->parent());
wifi->moveToThread(this->thread());

即使这也没有影响,如果我在wifi:初始化后添加这些行

wifi = new WirelessNet(this->parent());
QEventLoop testLoop;
testLoop.exec();

换句话说,"incoming"永远不会打印出来,所以我无法继续工作。有人知道吗,这几乎是教程中1:1的代码,这让我很困惑。

在您的主代码中:

WirelessNet *wifi = new WirelessNet(0); // 0 = assign no parent
QThread *wifiThread = new QThread;
wifi->moveToThread(wifiThread);
QObject::connect(wifiThread, SIGNAL(started()), wifi, SLOT(startWifi()));
// start() will start its own event loop, it will emit started(), therefore startWifi() slot will be called.
wifiThread->start();

然后您的WirelessNet类标题:

class WirelessNet : public QTcpServer
{
    Q_OBJECT
public:
    WirelessNet(QObject *parent = 0);
protected:
    void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
public slots:
    void startWifi();
};

然后您的WirelessNet类主体:

WirelessNet::WirelessNet(QObject *parent) : 
    QTcpServer(parent)
{
    // Do nothing much here because we want to initialise new stuff in our thread.
    // When this function runs we have not moved this to the new thread - or even started it.
}
void WirelessNet::incomingConnection(qintptr socketDescriptor)
{
    qDebug() << "incomming n";
    printf("incomming n");
    WirelessNetThread *thread = new WirelessNetThread(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}
// Called when the thread has started
void WirelessNet::startWifi()
{
    // Anything done here is now safely within out new thread.
    listen(QHostAddress::Any, 5220);
    printf("is listening %dn", this->isListening());
}

注意这是示例代码,我直接将其写入堆栈溢出,但尚未编译,因此可能存在一些错误:)我已经评论了一些关键点,您可能在最初的尝试中出错了。