Results 1 to 15 of 15

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2016
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    I sticked to the tutorial about threaded qt-networking (which is here: http://doc.qt.io/qt-5/qtnetwork-thre...r-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

    Qt Code:
    1. #include <QtNetwork>
    2. #include <QTcpServer>
    3. #include <QTcpSocket>
    4. #include <QThread>
    5.  
    6. class WirelessNetThread: public Thread
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. WirelessNetThread(int socketDescriptor, QObject * parent);
    12.  
    13. void run() Q_DECL_OVERRIDE;
    14.  
    15. signals:
    16. void error(QTcpSocket::SocketError socketError);
    17.  
    18. private:
    19. int socketDescriptor;
    20. QString text;
    21. };
    22.  
    23. class WirelessNet : public QTcpServer
    24. {
    25. Q_OBJECT
    26.  
    27. public:
    28. WirelessNet(QObject *parent = 0);
    29.  
    30. protected:
    31. void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
    32.  
    33. };
    To copy to clipboard, switch view to plain text mode 

    And the .cpp

    Qt Code:
    1. WirelessNetThread::WirelessNetThread(int socketDescriptor, QObject *parent):QThread(parent), socketDescriptor(socketDescriptor)
    2. {
    3. }
    4.  
    5. void WirelessNetThread::run()
    6. {
    7. QTcpSocket tcpSocket;
    8. if ( !tcpSocket.setSocketDescriptor(socketDescriptor))
    9. {
    10. emit error(tcpSocket.error());
    11. return;
    12. }
    13.  
    14. tcpSocket.disconnectFromHost();
    15. tcpSocket.waitForDisconnected();
    16. }
    17.  
    18. WirelessNet::WirelessNet(QObject *parent): QTcpServer(0)
    19. {
    20. listen(QHostAddress::Any, 5220);
    21. printf("is listening %d\n", this->isListening());
    22. }
    23.  
    24. void WirelessNet::incomingConnection(qintptr socketDescriptor)
    25. {
    26. qDebug() << "incomming \n";
    27. printf("incomming \n");
    28. WirelessNetThread *thread = new WirelessNetThread(socketDescriptor, this);
    29. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    30. thread->start();
    31. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. WirelessNet* wifi = new WirelessNet(this->parent());
    2. wifi->moveToThread(this->thread());
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. wifi = new WirelessNet(this->parent());
    2. QEventLoop testLoop;
    3. testLoop.exec();
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by QtExchange; 24th March 2016 at 12:10.

Similar Threads

  1. QTcpServer - how to refuse incomming connection
    By atomic in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2015, 13:31
  2. Replies: 1
    Last Post: 14th March 2014, 06:29
  3. Replies: 8
    Last Post: 11th September 2010, 10:41
  4. Replies: 1
    Last Post: 4th February 2009, 01:53
  5. Drag incomming localurl !=linux not work kde
    By patrik08 in forum Qt Programming
    Replies: 6
    Last Post: 11th December 2006, 15:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.