Briefly
In network operation, the network connection time-out is often caused by various reasons, what is the network connection timeout?
Network connection timeout: No response from the server within the program's default wait Time
- Briefly
- Reason for timeout
- Network connection timeout in Qt
Reason for timeout
There are a number of reasons why network connections time out, and here are some common causes:
- The network is disconnected, but it often shows that it cannot connect
- Network blocking, which prevents you from getting reply packets within the program's default wait Time
- Network is not stable, the network can not fully transfer server information
- system problems, system resources are too low to provide sufficient resources for the program to process server information
- Equipment is not stable, such as cable loose, interface is not plugged in and so on
- The system is busy when the network is registered, unable to respond
- Slow speed, such as the use of BT multi-threaded download, online watch video and other bandwidth-intensive software, if you use shared bandwidth to prevent other people's malicious consumption of bandwidth
- Computer infected with malicious software, computer viruses, Trojan horses, etc.
Network connection timeout in Qt
In Qt, there is no way to set timeouts for Qnetworkaccessmanager, Qnetworkrequest, and qnetworkreply. In this case, we can only deal with it ourselves.
Solution Ideas:
- Use Qtimer to start a single timer and set the time-out.
- After the event loop exits, the state of the timer is determined, and if it is activated, the request is completed; otherwise, the description times out.
To see a simple example, get the QT website content:
QTimer timer;
Timer. Setinterval (30000); / / set a timeout of 30 seconds
timer.setSingleShot(true);
//Request QT official website
QNetworkAccessManager manager;
QNetworkRequest request;
request.setUrl(QUrl("http://qt-project.org"));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
QNetworkReply *pReply = manager.get(request);
QEventLoop loop;
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
connect(pReply, SIGNAL(finished()), &loop, SLOT(quit()));
timer.start();
Loop. Exec(); / / start the event loop
If (timer. Isactive()) {/ / process the response
timer.stop();
if (pReply->error() != QNetworkReply::NoError) {
//Error handling
qDebug() << "Error String : " << pReply->errorString();
} else {
QVariant variant = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
int nStatusCode = variant.toInt();
//Further data processing according to the status code
//QByteArray bytes = pReply->readAll();
qDebug() << "Status Code : " << nStatusCode;
}
}Else {/ / processing timeout
disconnect(pReply, SIGNAL(finished()), &loop, SLOT(quit()));
pReply->abort();
pReply->deleteLater();
qDebug() << "Timeout";
}
First, define a Qtimer, and then set the time-out to 30000 milliseconds (30 seconds). Then, use Qnetworkrequest to implement a simple network request, starting with Qnetworkaccessmanager::get () to get the content of the HTML page of the Qt website. Because the request process is asynchronous, you start an event loop by using Qeventloop and connect the Qtimer timeout () signal and the qnetworkreply finished () signal to its quit () slot function to ensure that after the timer expires or The event loop exits after the network response is complete, not always in a blocked state.
As mentioned above, there are two situations when an event loop exits:
- Qtimer 30 seconds expired, timed out
- Network Connection Response Complete
Therefore, when qtimer::isactive () is active, it proves that the response is complete and has not yet timed out. At this point you need to call Qtimer::stop () to stop the timer, and then do further processing of the ring. Otherwise, time-out processing-qnetworkreply::abort () aborts the operation immediately and shuts down the network connection.
QT processing Qnetworkaccessmanager Network connection timeout