Results 1 to 15 of 15

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

Hybrid 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 13:10.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by QtExchange View Post
    here the excerpt out of my main program, where it is initiated (by the way it doesn't matter if I leave out moveToThread()):
    That is quite understandable as it would move the object to the thread it is already on, i.e. no change.

    Given that WirelessGloveThread::run() is probably actually WirelessNetThread::run() and that wifi is hopefully actually a pointer, I wonder how much else of that is not actually your code.

    Can we at least assume that your actual code is reporting "true" for isListening()?
    Is the wifi object an object of the main thread?
    Is the application's event loop running?
    If you connect to the newConnection() signal, is the slot invoked?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2016
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Of course you were right, I had to type it by hand since the working machine has no internet connection, so these were just typos.

    And yes everytime the line "is listenning 1" is printed out, so isListenning is True.

    the eventloop should be running (I'm not sure how to check this), however when adding an extra QEventLoop + exec() as mentioned in the post it doesn't change the situation either

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    And does the signal get emitted?

    Cheers,
    _

  5. #5
    Join Date
    Mar 2016
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Alternatively I tried to catch the SIGNAL(newConnection()) instead of the overridden incommingConnection() on server side. But as mentioned above this signal never gets fired or at least not processed.

    On client side I surprisingly have absolutely no problem connecting to this server, after I executed its listen() - but still no reaction on server side

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by QtExchange View Post
    Alternatively I tried to catch the SIGNAL(newConnection()) instead of the overridden incommingConnection() on server side. But as mentioned above this signal never gets fired or at least not processed.
    Ah, sorry, didn't see that.

    Quote Originally Posted by QtExchange View Post
    On client side I surprisingly have absolutely no problem connecting to this server, after I executed its listen() - but still no reaction on server side
    Hmm, maybe the client is connecting to something else?

    Cheers,
    _

  7. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

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

    In your original post, you show a run() method for your thread that does not contain a message loop and will exit almost immediately after your thread is started. The default QThread::run() runs a message loop. If you write your thread to persist and continue to run, you should execute the base class QThread::run() in your subclassed run() method or just simply call exec() in your run() method.

    Without this, your thread will start and end almost instantaneously, which I'm sure is not what you intended.
    Last edited by jefftee; 25th March 2016 at 23:17.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  8. #8
    Join Date
    Mar 2016
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Please pardon my late reply for I have not been at the desk during the holidays.

    Quote Originally Posted by anda_skoa View Post
    Hmm, maybe the client is connecting to something else?
    It's a wireless network where there is only 1 host & 1 client. The client is not able to connect if the host hasn't reached the line listen()

    Quote Originally Posted by jefftee View Post
    My advice? Learn to walk before you try to run.
    That's why I stick to the tutorial made directly by Qt, how does one learn the very lowest instance of a teaching unit if even this ain't running.

    Quote Originally Posted by jefftee View Post
    In your original post, you show a run() method for your thread that does not contain a message loop and will exit almost immediately after your thread is started. The default QThread::run() runs a message loop. If you write your thread to persist and continue to run, you should execute the base class QThread::run() in your subclassed run() method or just simply call exec() in your run() method.

    Without this, your thread will start and end almost instantaneously, which I'm sure is not what you intended.
    This might be true. However I don't even get to the point where this thread is created - which is my actual problem mentioned in the top post. As stated incomingConnection(), where this thread first is created, is never fired.
    Last edited by QtExchange; 30th March 2016 at 09:38.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by QtExchange View Post
    It's a wireless network where there is only 1 host & 1 client. The client ain't able to connect if the host hasn't reached the line listen()
    I did not mean another host, I meant another program.
    If you have more than one interface, say actual network and loopback and another program is listening on the same port on loopback, your program will still be able to listen on the wifi network interface, but the client might connect to the server listening on loopback.

    Cheers,
    _

  10. #10
    Join Date
    Mar 2016
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by anda_skoa View Post
    I did not mean another host, I meant another program.
    If you have more than one interface, say actual network and loopback and another program is listening on the same port on loopback, your program will still be able to listen on the wifi network interface, but the client might connect to the server listening on loopback.
    Well it is only working after listen(). In general I don't consider this as a problem, since I switched from windows libraries to Qt for networking and kept the settings (ports & addresses). With these libraries everything worked like charm, but now I switched to Qt for ensuring mulitple platform compatibility.

  11. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

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

    Why don't you post your entire project so we can see the code in its entirety or alternatively a compilable example that demonstrates the problem?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  12. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

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

    Quote Originally Posted by QtExchange View Post
    Somebody knows what's going wrong?
    My advice? Learn to walk before you try to run. Qt's networking classes are asynchronous and don't require multi-threading to work properly.

    If you've read how to properly do threading in Qt, people typically use one of the 1) moveToThread method or 2) subclass QThread and implement your code in the QThread::run() method. You seem to want to do both, which I think demonstrates a lack of understanding regarding how to properly do multi-threading in Qt.

    Scrap the threading altogether and start with a simple QTcpServer that can accept incoming connections, handle reading/writing data to the socket, and handle client disconnects, etc. Do that first before you try to introduce multiple threads or add any other features to your application.

    Good luck.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  13. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

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

    Quote Originally Posted by jefftee View Post
    If you've read how to properly do threading in Qt, people typically use one of the 1) moveToThread method or 2) subclass QThread and implement your code in the QThread::run() method. You seem to want to do both, which I think demonstrates a lack of understanding regarding how to properly do multi-threading in Qt.
    Can you explain why you are opposed to following the best practices when it comes to multi-threading in Qt? Subclassing QThread *and* using the moveToThread approach is not something that you'll find in any (working) example.

    Glad that you have it working for now, but I would still personally adopt one of the QThread subclass approach or the moveToThread approach, not both.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. QTcpServer - how to refuse incomming connection
    By atomic in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2015, 14:31
  2. Replies: 1
    Last Post: 14th March 2014, 07:29
  3. Replies: 8
    Last Post: 11th September 2010, 11:41
  4. Replies: 1
    Last Post: 4th February 2009, 02:53
  5. Drag incomming localurl !=linux not work kde
    By patrik08 in forum Qt Programming
    Replies: 6
    Last Post: 11th December 2006, 16: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.