Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtNetwork: Why don't I detect incomming connections ? (incomingConnection() is never fired)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
qtnetworkqthreadqtcpserver
4 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtExchange
    wrote on last edited by QtExchange
    #1

    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.

    Kind Regards

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QtExchange
      wrote on last edited by
      #2

      nobody knows about this strange Qt-behavior ?

      kshegunovK 1 Reply Last reply
      0
      • Q QtExchange

        nobody knows about this strange Qt-behavior ?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @QtExchange
        What's the debug output? Do you mind uploading the whole piece of code, as this:

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

        doesn't mean much to me without knowing what this is ... neither does the following snippet:

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

        (same reason, plus I can't fathom why QEventLoop is needed).

        If I had to guess, possibly this:

        listen(QHostAddress::Any, 5220);
        

        shouldn't be in the constructor, but probably would work anyway.

        PS.
        That (fortune teller) example should really, really be removed from the documentation ... or at least rewritten ...

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          QtExchange
          wrote on last edited by QtExchange
          #4

          It is working now and the reason was the listen() was in the wrong position and you need an additional Signal/Slot connection from main program to the network extension as following:

          the main program gets changed like this:

          wifi= new WirelessNet(0);
          QThread *thread = new QThread;
          wifi->moveToThread(thread);
          connect(thread,SIGNAL(started()), wifi,SLOT(initWifi()));
          thread->start();
          

          the constructor of WirelessNet is now empty, therefore it gets a new public slot function:

          void WirelessNet::initWifi()
          {
          	listen(QHostAddress::Any, 5220);
          }
          

          And that's the trick.

          If one is creating a WirelessNet-Instance, starts listenting inside the constructor but first then uses the movetoThread(), the connection is lost.

          Concluding in one sentence: First move to the right thread and afterwards start listening

          Thanks for your help. Let me tell you I hate the official Qt-""Tutorials""

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved