Edgarth
Edgarth

Reputation: 122

Can't set timeout for Qt's waitForConnected

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;

Upvotes: 1

Views: 5121

Answers (3)

mic
mic

Reputation: 201

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.

Upvotes: 2

Antwane
Antwane

Reputation: 22688

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?

Upvotes: 4

francesco
francesco

Reputation: 1

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.

Upvotes: 0

Related Questions