Results 1 to 6 of 6

Thread: Wait for socket to connect without application hang?

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Wait for socket to connect without application hang?

    Hello, that's the code:
    Qt Code:
    1. sock.connectToHost("www.gamato.info", 80);
    2. if (sock.waitForConnected(15000))
    3. qDebug("Connected!");
    4. else
    5. qDebug("Not connected!");
    To copy to clipboard, switch view to plain text mode 
    I chose gamato.info as it is an unresponsive server and I noticed that the whole application hangs for 15 seconds.
    The function in which this runs is static and cannot be run through QtConcurrentRun!
    The actual problem is checking if internet connection is established or not, but this way I take the non-rare situation in which internet connection may be established locally but modem not connected etc.
    I need a way
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Wait for socket to connect without application hang?

    It's no surprise that app hangs for 15 seconds because You designed it that way.
    I personally would use code posted by You only in situation when I need measure proxy speed with wait condition set to i.e. 250 ms. And even in that situation better way is to use timer and signal/slot mechanism, because using it in thread is just pain.

    Use signal and slot mechanism (i.e. socket->hostFound or better socket->stateChanged) to implement non-blocking socket!
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. The following user says thank you to Talei for this useful post:

    hakermania (11th September 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Wait for socket to connect without application hang?

    Thanks, this was helpful and quite easy to implement, but I have a problem, this is my boolean function that is called from another pieces of code. It is to return true if there's connection (it manages to connect to the specified host) and is to return false if it doesn't manage to connect to the specified host.
    This *was* the code that hanged but worked fine (I had the opportunity to return true or false)
    Qt Code:
    1. bool Global::connected_to_internet(){
    2. QTcpSocket sock;
    3. sock.connectToHost("www.gamato.info", 80);
    4. if (sock.waitForConnected(15000))
    5. return true;
    6. else
    7. return false;
    8. }
    To copy to clipboard, switch view to plain text mode 

    The piece of code using your suggestion will be:
    Qt Code:
    1. bool Global::connected_to_internet(){
    2. sock = new QTcpSocket(this);
    3. QObject::connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(check_what_happened(QAbstractSocket::SocketType)) );
    4. sock.connectToHost("www.gamato.info", 80);
    5. }
    6.  
    7. void Global::check_what_happened(QAbstractSocket::SocketType socktype){ //< it goes through this function while the bollean is over
    8.  
    9. switch(socktype){
    10.  
    11. case QAbstractSocket::hostFound():
    12.  
    13. //etc
    14.  
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    I don't know how to work this out. I need my boolean function to return true or false and by calling another void function (maybe can it be boolean and somehow grab true or false or to emit a signal?) I cannot alert somehow if I could connect to the host or not... :/
    Last edited by hakermania; 11th September 2011 at 08:37.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Wait for socket to connect without application hang?

    This is strict alog. implementation issue and can be resolved in many ways.
    I would make global var bool m_gotConnection = false;, then change that variable on sign. socket->stateChange, i.e gotConnection = true; then I would create QTimer and on timeout i would check if gotConnection i true or not and see how many ms passed out.

    Pseudo code:
    Qt Code:
    1. bool m_gotConn = false;
    2. bool Global::connected_to_internet()
    3. {
    4. QTcpSocket *sock = new QTcpSocket(this);
    5. QTimer *timer = new QTimer(this);
    6.  
    7. connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(check_what_happened(QAbstractSocket::SocketType)) );
    8. connect( timer, SIGNAL(timeout()), this, SLOT( checkConnState() ));
    9. m_gotConn = false;
    10. timer->setInterval( 15000 );
    11.  
    12. sock.connectToHost("www.gamato.info", 80);
    13. timer->start();
    14. }
    15.  
    16. void Global::check_what_happened(QAbstractSocket::SocketType socktype)
    17. {
    18. switch(socktype){
    19. case QAbstractSocket::hostFound(): {
    20. m_gotConn = true;
    21. // host connected, stop timer
    22. timer->stop();
    23. }break;
    24.  
    25. //etc
    26.  
    27. }
    28. }
    29.  
    30. void Global::checkConnState()
    31. {
    32. //Not connected after 15 sec
    33. if( !m_gotConn ) {
    34. socket->abort();
    35. }else{
    36. //Connected after 15 sec
    37. // this should be never reached in curr. impl but just to show idea
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  6. The following user says thank you to Talei for this useful post:

    hakermania (12th September 2011)

  7. #5
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Wait for socket to connect without application hang?

    one word: QNetworkConfigurationManager::isOnline();
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wait for socket to connect without application hang?

    Quote Originally Posted by hakermania View Post
    one word: QNetworkConfigurationManager::isOnline();
    This won't tell you if you're connected to the Internet. It will only tell you whether the link is up or down.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 19th March 2011, 05:02
  2. Replies: 1
    Last Post: 12th October 2010, 22:38
  3. Wait in thread till QTcp socket have some thing to read
    By hasnain in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2010, 12:46
  4. wait the application for some time..
    By mohanakrishnan in forum Qt Programming
    Replies: 3
    Last Post: 9th December 2009, 08:22
  5. Replies: 4
    Last Post: 10th November 2006, 15:38

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.