不能为Qt的waitForConnected设置超时时间?

时间:2023-01-29 21:12:50

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).

我正在尝试在Qt应用程序中设置连接超时,但是不管我将哪个数字作为参数传递给waitForConnected,超时都是相同的(大约3秒,而不是默认的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?

不管我给koko设置了什么号码,超时时间都在3秒左右。我做错了什么?

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):

而koko正在从QLineEdit中获取(超时是QLineEdit):

int koko = ui->Timeout->text().toInt()*1000;

3 个解决方案

#1


3  

From the Qt documentation for QAbstractSocket:

从Qt的Qt文档到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.

等待直到套接字连接,直到msecs毫秒。如果已建立连接,则该函数返回true;否则返回false。

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

您说该方法在大约3秒后返回false。这可能是正常的行为。看到这段代码:

#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.

在所有情况下,waitForConnected()方法返回false。

  • Firstly, the address (127.0.0.1) is reachable but the port is closed: the connection fails immediately.
  • 首先,地址(127.0.0.1)是可访问的,但是端口是关闭的:连接立即失败。
  • 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)
  • 然后,该地址存在(在同一网络上),但是需要更多的时间来检测端口是否已关闭。6秒后就失效了
  • Finally, 192.168.1.0 isn't reachable, so the full timeout is necessary to ensure the connection has failed.
  • 最后,192.168.1.0无法访问,因此需要完全超时以确保连接失败。

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

请记住另一个重要的信息(仍然来自Qt文档):

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

注意:此函数可能在Windows上随机失败。如果您的软件在Windows上运行,请考虑使用事件循环和连接()信号。

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

这也可能是你的问题。你在窗户上跑步吗?

#2


1  

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.

它主动尝试连接4秒。您可能想要更改参数,例如timeout=200;maxtrials=150,等待30秒。

#3


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.

方法返回的是真还是假?如果它返回true,则连接已经建立。根据文档,waitForConnected()会等待30秒的连接,但是如果连接已经建立,它将直接返回。

#1


3  

From the Qt documentation for QAbstractSocket:

从Qt的Qt文档到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.

等待直到套接字连接,直到msecs毫秒。如果已建立连接,则该函数返回true;否则返回false。

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

您说该方法在大约3秒后返回false。这可能是正常的行为。看到这段代码:

#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.

在所有情况下,waitForConnected()方法返回false。

  • Firstly, the address (127.0.0.1) is reachable but the port is closed: the connection fails immediately.
  • 首先,地址(127.0.0.1)是可访问的,但是端口是关闭的:连接立即失败。
  • 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)
  • 然后,该地址存在(在同一网络上),但是需要更多的时间来检测端口是否已关闭。6秒后就失效了
  • Finally, 192.168.1.0 isn't reachable, so the full timeout is necessary to ensure the connection has failed.
  • 最后,192.168.1.0无法访问,因此需要完全超时以确保连接失败。

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

请记住另一个重要的信息(仍然来自Qt文档):

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

注意:此函数可能在Windows上随机失败。如果您的软件在Windows上运行,请考虑使用事件循环和连接()信号。

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

这也可能是你的问题。你在窗户上跑步吗?

#2


1  

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.

它主动尝试连接4秒。您可能想要更改参数,例如timeout=200;maxtrials=150,等待30秒。

#3


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.

方法返回的是真还是假?如果它返回true,则连接已经建立。根据文档,waitForConnected()会等待30秒的连接,但是如果连接已经建立,它将直接返回。