PDA

View Full Version : QAbstractSocket::waitForConnected() does not waits



babu198649
24th January 2009, 08:33
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.


#include <QtCore>
#include <QtNetwork>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QTcpSocket socket;
socket.connectToHost(QString("127.0.0.1"), 8888);
if(socket.waitForConnected(-1))
qDebug()<<"wait connected";

qDebug()<<socket.state();

return a.exec();
}


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.

jpn
24th January 2009, 12:08
What does error() return?

babu198649
24th January 2009, 12:27
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.

jpn
24th January 2009, 13:27
What's the point of waiting if the connection gets refused?

babu198649
25th January 2009, 05:40
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.