1

I'm trying to make connection timeout in my Qt application customisable, but no matter what number I pass as argument to waitForConnected, the timeout is the same (and it's around 3 seconds, not the default 30).

Example:

if(socket->waitForConnected(koko))
{
    ...do stuff...
}
else
{
    ...do else stuff...
}

No matter what number I set koko to, the timeout keeps being around 3 seconds. What am I doing wrong?

My socket connection:

socket = new QTcpSocket();
socket->connectToHost(addres,port);

where:

QHostAddress addres, quint16 port

and koko im gaining from QLineEdit like that (Timeout is QLineEdit):

int koko = ui->Timeout->text().toInt()*1000;
Edgarth
  • 124
  • 1
  • 12
  • Are you sure you tried to connect to a non reachable host before saying the timeout doesn't work? The code sample you provided isn enough helpful. Please add at least the initialization of `socket` (which tell us the concrete type of the QAbstractSocket) and the parameters you put in `connectToHost()` – Antwane Nov 24 '14 at 22:18
  • @Antwane - he says timeout is 3 seconds, that implies the socket indeed times out rather than succeeding in the connection attempt. – dtech Nov 25 '14 at 00:46
  • @ddriver so it's a normal behavior. The timeout runs only while the connection is in HostLookupState or ConnectingState after connectToHost() has been called. If the connection fail (UnconnectedState), the method returns false. If the connection succeed (ConnectedState), the method returns true. The doc is clear about that: `Waits until the socket is connected, UP TO *msecs* milliseconds`. If the connection state change before 30 seconds (default), the method will return earlier. – Antwane Nov 25 '14 at 08:04
  • Im sure that its returning false got else in if and it is always doing else stuff, i added how i am connecting to socket. – Edgarth Nov 25 '14 at 11:28
  • For testing purposes, don't use `ui->Timeout` to get the value of `koko` -- it's just another potential source of error. Use `koko = 30000;` instead. – TonyK Nov 25 '14 at 13:12

3 Answers3

4

From the Qt documentation for QAbstractSocket:

Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false.

You said the method returns false after about 3 seconds. It could be a normal behaviour. See this code:

#include <QTcpSocket>
#include <QTime>

int main(int, char *) {
    QStringList hosts;
    hosts << "127.0.0.1" << "10.1.25.62" << "192.168.1.0";
    for(QString host : hosts) {
        QTime timer;
        timer.start();

        QTcpSocket socket;
        socket.connectToHost(host, 80);
        if(socket.waitForConnected(30000)) {
            qDebug() << host << "-- Connected in" << timer.elapsed();
        } else {
            qDebug() << host << "-- NOT Connected in" << timer.elapsed();;
        }
    }
}

The result is:

"127.0.0.1" -- NOT Connected in 1
"10.1.25.62" -- NOT Connected in 5997 
"192.168.1.0" -- NOT Connected in 30020

In all cases, the waitForConnected() method returns false.

  • Firstly, the address (127.0.0.1) is reachable but the port is closed: the connection fails immediately.
  • Then, The address exists (on the same network), but it take a bit more time to detect that the port is closed. It fails after 6 sec (about)
  • Finally, 192.168.1.0 isn't reachable, so the full timeout is necessary to ensure the connection has failed.

Please keep in mind another important information (still from the Qt documentation):

Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows.

It can also be your issue. Do you run on Windows?

Antwane
  • 20,760
  • 7
  • 51
  • 84
2

I usually do this so:

int iTrial=0;
int iMaxTrials=200;
int iTimeOut=20;

do {
    pTcpSocket->connectToHost(QHostAddress::LocalHost,uPort);
} while (!pTcpSocket->waitForConnected(iTimeOut) && ++iTrial < iMaxTrials);

It actively tries to connect for 4 seconds. You may want to change the parameters to e.g. timeout=200; maxtrials=150 to wait for 30 seconds.

mic
  • 201
  • 2
  • 4
0

Did the method returned true or false? If it returned true, the connection has been established. According to the documentation, waitForConnected() waits up to 30 seconds for the connection, but if the connection has been established before, it returns directly.

Antwane
  • 20,760
  • 7
  • 51
  • 84
francesco
  • 1
  • 3
  • I'm sure that it's returning false, problem here like i said before is that wait time is always abaut 3s no mater what i put in koko. – Edgarth Nov 25 '14 at 11:26