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

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

我坚持使用关于线程 qt-networking 的教程(在此处: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 %d\n", 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();

换句话说,"incomming" 永远不会打印出来,所以我无法继续工作。有没有人有想法,这几乎是 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 header:

class WirelessNet : public QTcpServer
{
    Q_OBJECT

public:
    WirelessNet(QObject *parent = 0);

protected:
    void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;

public slots:
    void startWifi();
};

那么你的 WirelessNet class body:

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 %d\n", this->isListening());
}

注意这是示例代码,我直接写进了stack overflow 没有编译,所以可能有一些错误:)有一些关键点,我已评论,您可能在最初的尝试中出错了。