Results 1 to 8 of 8

Thread: Networking: QTcpSocket timeout

  1. #1
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Networking: QTcpSocket timeout

    How can I change the connect timeout on a QTcpSocket? My application talks to other systems on the same subnet so I need to modify the timeout down to about 200ms.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Networking: QTcpSocket timeout

    does QAbstractSocket::waitForConnected() not do what you want?

  3. #3
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Networking: QTcpSocket timeout

    Do you know if waitForConnected() is blocking or does it call Application::ProcessEvents()?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Networking: QTcpSocket timeout

    It waits the specified amount of milliseconds before returning, so it's blocking.

  5. #5
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Networking: QTcpSocket timeout

    But while it is blocking it can make calls to qApp.ProcessMessages() to keep the GUI responsive. I wonder if it does that? I guess I will check.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Networking: QTcpSocket timeout

    I very much doubt it will do that. If you want that functionality, put it into a thread so you main gui thread continues to run.

  7. #7
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Networking: QTcpSocket timeout

    I found that it does not "refresh" the GUI.

    Rather than introducing a new thread I wrote this routine that "refreshes" the GUI and returns true on connect and false on a timeout.

    Qt Code:
    1. bool Connector::waitForConnection(quint16 ms)
    2. {
    3. QTime myTimer;
    4. myTimer.start();
    5. do {
    6. qApp->processEvents();
    7. if (tcpSocket.state() == QAbstractSocket::ConnectedState)
    8. return true;
    9. } while(myTimer.elapsed() < ms);
    10. return false;
    11. }
    To copy to clipboard, switch view to plain text mode 

    I could of used a QTimer and used the Connect SIGNAL but I was concerned about what could happen if I got a Connect SIGNAL at the same time that the Timer Exprires.

    It would be simplest if the QTcpSocket had a timeout parameter; but that is where this thread started.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Networking: QTcpSocket timeout

    I hope your timeout is short, as your waitForConnection() method will use up 100% CPU usage until the timeout or the connection occurs. What would be better is to use an asynchronous function call - ie, as you said, a QTimer whilst waiting for the connect signal. You could of course check the connection state in your timer to just check if both signals occured at once, but the chance of that happening is slim.

Similar Threads

  1. QT non-gui networking
    By MAbeeTT in forum Newbie
    Replies: 1
    Last Post: 21st August 2009, 19:47
  2. QTcpSocket timeout problem
    By altVis in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 13:56
  3. QTcpSocket + timeout
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2008, 13:38
  4. Qt-3 and networking
    By a550ee in forum Qt Programming
    Replies: 3
    Last Post: 3rd October 2006, 11:15
  5. Networking Help please
    By munna in forum Newbie
    Replies: 1
    Last Post: 17th September 2006, 09:35

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
  •  
Qt is a trademark of The Qt Company.