Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager and multiple connections
QtWS25 Last Chance

QNetworkAccessManager and multiple connections

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveyDave123
    wrote on 2 Aug 2013, 15:55 last edited by
    #1

    Hi All,
    I'm writing an application that needs to make multiple connections to multiple servers using QNetworkAccessManager. Basically, if I try multiple post() calls all at once then I typically receive UnknownNetworkError errors which I believe are because I am exceeding more than 6 simultaneous connections (which I believe is the limit for QNetworkAccessManager).

    So my questions are:
    1). Can I remove the 6 connections limit?.

    2). Can I have multiple QNetworkAccessManager instances in the same thread and control which one I call post on?.

    3). How can I tell how many connections a QNetworkAccessManager currently has?. I would need to do this so I could tell which instance has spare capacity.

    I could try to serialize up all of the connections but I don't think that's a good approach when dealing with multiple servers.
    Any other pointers would be useful.

    Ta in advance,
    Dave

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 3 Aug 2013, 02:59 last edited by
      #2

      The limit is 6 connections per host/port pair. Netiquette limits the number of simultaneous connections. Six is what most browsers allow, and even that is higher than the "RFC 2616":http://www.w3.org/Protocols/rfc2616/rfc2616.html recommended value of 2. For sites like map tile servers (that are actually server farms) you will often see a set of hosts, e.g. a, b, c, and d.example.com, specifically so the Javascript can end-run the connection limit in the browser.

      The number of connections is not the number of outstanding requests. QNAM queues requests and requests can also be pipelined on a single connection. I can happily issue 100 requests to seven different Google severs in a tight loop and they will all execute without error.
      @
      #include <QtCore>
      #include <QtNetwork>

      class Fetcher: public QObject {
      Q_OBJECT
      public:
      Fetcher(QObject p = 0): QObject(p) {
      connect(&nam, SIGNAL(finished(QNetworkReply
      )), SLOT(slotFinished(QNetworkReply*)));
      }

      void fetch(const QUrl &url) {
          QNetworkReply *reply = nam.get(QNetworkRequest(url));
      }
      

      public slots:
      void slotFinished(QNetworkReply *reply) {
      qDebug() << Q_FUNC_INFO << "Error" << reply->error() << reply->request().url();
      reply->deleteLater();
      }

      private:
      QNetworkAccessManager nam;
      };

      int main(int argc, char **argv) {
      QCoreApplication app(argc,argv);
      Fetcher f;
      for(int i = 0; i < 100; ++i) {
      f.fetch(QUrl(QString("http://www.google.com?xyzzy=%1").arg(i)));
      f.fetch(QUrl(QString("http://www.google.com.au?xyzzy=%1").arg(i)));
      f.fetch(QUrl(QString("http://www.google.co.uk?xyzzy=%1").arg(i)));
      f.fetch(QUrl(QString("http://www.google.co.jp?xyzzy=%1").arg(i)));
      f.fetch(QUrl(QString("http://www.google.pl?xyzzy=%1").arg(i)));
      f.fetch(QUrl(QString("http://www.google.fr?xyzzy=%1").arg(i)));
      f.fetch(QUrl(QString("http://www.google.de?xyzzy=%1").arg(i)));
      }
      return app.exec();
      }
      @

      I would say your UnknownNetworkError is something else... possibly a server or firewall defending against abuse or wholesale harvesting.

      1) Yes, but you will have to modify Qt, rebuild the library, and live with your lack of nettiquette.
      2) Yes. Will that circumvent the connection count? I don't know.
      3) There's no public interface to that.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DaveyDave123
        wrote on 5 Aug 2013, 07:46 last edited by
        #3

        Thanks for your reply Chris. So are you saying that I should be able to execute as many ->post() calls as I like and NAM will queue and serialize them such that no more than 6 simultaneous connections are made?.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DaveyDave123
          wrote on 5 Aug 2013, 09:14 last edited by
          #4

          OK, I've confirmed it doesn't work like that. Analysis with WireShark shows that the NAM is not sending a lot of the post requests. The ones it fails to send result in 'Unknown Error's in the QNetworkReply error.

          Consequently a NAM problem.

          Any ideas?.

          1 Reply Last reply
          0

          1/4

          2 Aug 2013, 15:55

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved