Results 1 to 5 of 5

Thread: QAbstractSocket::waitForConnected() does not waits

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QAbstractSocket::waitForConnected() does not waits

    Hi
    The Qt doc says that QAbstractSocket::waitForConnected(-1) will wait until the connection is made.But it returns immediately.The following program shows the problem.

    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. QTcpSocket socket;
    9. socket.connectToHost(QString("127.0.0.1"), 8888);
    10. if(socket.waitForConnected(-1))
    11. qDebug()<<"wait connected";
    12.  
    13. qDebug()<<socket.state();
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    In the above program the if condition is supposed to return always true.But it always returns immediately and the socket state is in unconnected state.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractSocket::waitForConnected() does not waits

    What does error() return?
    J-P Nurmi

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractSocket::waitForConnected() does not waits

    error() returns AbstractSocket::ConnectionRefusedError and errorString() returns
    "Connection refused".

    After some trial ,When i changed the host to remote host instead of local host i.e.
    socket.connectToHost(QString("192.168.1.5"), 8888);
    the function waits as expected.Is this a error in the waitForConnected function.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractSocket::waitForConnected() does not waits

    What's the point of waiting if the connection gets refused?
    J-P Nurmi

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QAbstractSocket::waitForConnected() does not waits

    I want a client application which keeps trying till it gets connected to server.

    I dont know who is refusing the connection since no server is listening to port 8888(to which client is trying to connect).

    Although i use while loop to reconnect as a workaround,the while makes the application run slower.

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.