QT processing Qnetworkaccessmanager Network connection timeout

Source: Internet
Author: User


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()), &amp;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:


    1. Qtimer 30 seconds expired, timed out
    2. 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.