So I'm developing a web application for embedded devices that uses QNetworkAccessManager to retrieve information from a web server. Each request is a GET request calling a PHP page with some parameters in the url. The output of the PHP script is the info my app is requesting.
The whole process is very simple and can be simplified down to these lines of code :
void fetch() {
Q_ASSERT(m_reply == 0);
QUrl url = QUrl("http://subdomain.myserver.com/info.php?param1=1¶m2=2");
m_reply = m_qnam->get(url);
connect (m_reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect (m_reply, SIGNAL(finished()), this, SLOT(onFinished()));
}
void onReadyRead() {
m_contents.append(m_reply->readAll());
}
void onFinished() {
m_reply->deleteLater();
m_reply = 0;
// do something with the contents
m_contents.clear();
// fetch again
fetch();
}
m_qnam is a QNetworkAccessManager instance and m_contents is a QByteArray.
This works great, however I encounter problems when attempting hundreds of such requests where I end up receiving a 429 error (Too many connections from the same IP) from the server (Apache). To recover from that I have to disconnect from the internet and reconnect again (which gives me a new IP address).
I could increase the number of maximum connections on Apache, however I believe that the TCP connections should be closed as soon as the QNetworkReply is deleted but this doesn't seem to be the case. Since I'm only downloading a constant number of requests at a time (this is controlled by a queueing system on my app and QNetworkAccessManager also has a maximum number of simultaneous downloads), I don't believe tweaking Apache is the answer.
It seems that the TCP connections are not closed properly.
So my question is :
how to make sure that each connection to my server is closed properly when the download is completed. Is QNetworkAccessManager supposed to handle this? Is this a bug? Should I make my own custom TCP connection to my server and keep a certain number of connections alive during my whole application? If so, where should I start?
Thanks, any help would be appreciated.
Qt 5.3/OSX/iOS
Note: modified my question to comply with Pavel's comment. There is only one request active at a time.
Answering my own question : QNetworkAccessManager closes correctly all the connections once the QNetworkReply is deleted, or the abort() or close() methods are called. Of course this happens in an asynchronous way.
The error I received came from some limitations imposed on my server which only allowed a certain number of consecutive connections coming from each IP.