I'm writing an application to download a file using Qt. The code snippet is
QNetworkAccessManager nam = new QNetworkAccessManager();
QNetworkRequest request;
request.setUrl(kUrl);
QNetworkReply reply;
//connect error(), finished(), downloadProgress(b,t) signal of reply object
reply = nam.get(request);
It works fine but under some circumstances in Mac, the downloads are stuck permanently without an error callback. Those situations are
- Pull out the LAN(internet) cable and insert it back quickly within 20 seconds
- Switch from Wired to WiFi within 10-20 seconds and viceversa
File Downloads are stuck in the above mentioned scenarios. Though, the frequency of this issue is once in 20 times(5%). To mitigate this, I wrote a workaround code to check whether network is up or down using TCP Socket and to poll it every 30 seconds.
bool check()
{
std::auto_ptr<QTcpSocket> sock(new QTcpSocket);
sock->connectToHost(kHostname, kConnectPort);
bool connected = sock->waitForConnected(30000);
if (!connected)
{
sock->abort();
return false;
}
sock->close();
return true;
}
It worked well for certain extent. But even with this, if 'LAN card pull and insert back' happens within 30 seconds, downloads are getting stuck again. So, this workaround couldn't solve my issue completely. Polling in very shorter interval doesn't looks like a proper solution as for any shorter time interval, there can be a race condition.
I'm stuck here in finding out a way to solve this downloads stuck in the middle. Can anyone of you please help in figuring out a way to notify the downloads instantaneously when network goes off?
The Qt version which I'm using is 5.3.1 and Mac OS is 10.9.