user3085931
user3085931

Reputation: 1803

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

I sticked to the tutorial about threaded qt-networking (which is here: http://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html), I made some minor changes and integrated it into my main program. However incomingConnection() never gets executed, on the other hand the client is able to connect. Since I'd like to work with incomingConnection() it became obsolete to work with the SIGNAL(newConnection()) but even this isn't working.

Somebody knows what's going wrong?

Here my .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;

};

And the .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();
}

here the excerpt out of my main program, where it is initiated (by the way it doesn't matter if I leave out moveToThread():

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

Even this has no influence if I add these lines after the initalization of wifi:

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

In other words "incomming" is never printed out, and so I'm not able to work on. Has anyone an idea, this is pretty much 1:1 the code from the tutorial that's what confuses me.

Upvotes: 1

Views: 374

Answers (1)

code_fodder
code_fodder

Reputation: 16381

In your main code:

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();

Then your 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();
};

Then your 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());
}

note this is example code, I wrote it directly into stack overflow it has not been compiled, so there are probably some errors :) There are some key points, that I have commented, where you may have gone wrong in your original attempt.

Upvotes: 2

Related Questions