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. "Connection closed" When using QNetworkAccessManager and QTcpServer
Qt 6.11 is out! See what's new in the release blog

"Connection closed" When using QNetworkAccessManager and QTcpServer

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 4.8k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    Ildar
    wrote on last edited by Ildar
    #1

    I have simple server with QTcpServer and simple client with QNetworkAccessManager.

    1. When I request data from the server via curl or browser everything is ok
    2. When I request data from any site via QNetworkAccessManager everything is ok
    3. But I can not read data from QTcpServer via QNetworkAccessManager. All requests are reseted. QNetworkAccessManager (client) had send RST (reset connection) right after it received a data from server. You could see the Wireshark logs. And in client code we get the error: "Connection closed" (RemoteHostClosedError)
    4. Aslo, I tried use QNetworkAccessManager from DownloadManager example and QTcpServer from FortuneServer example in various combinations, but the results were the same.

    Tested Qt Versions:

    • Mac Qt 5.7
    • Linux Qt 5.7
    • Linux Qt 5.6.2
    • Linux Qt 5.5.1

    Wireshark logs:
    https://drive.google.com/file/d/0B0Pb09eHyZTfUThQNTctQVJOUVU/view?usp=sharing

    Wireshark screenshot: qt-wireshark.png
    The upper parts (with red lines) are results of QNetworkAccessManager, and the latest packets with success result are curl attempt to get data from QTcpServer

    Also there is a simple example to reproduce the error: testNetwork.zip

    And here is sample code for client:

    void test(quint16 port)
    {
        QNetworkAccessManager *manager = new QNetworkAccessManager();
    
        QNetworkRequest request;
        request.setUrl(QUrl(QString("http://127.0.0.1:%1/").arg(port)));
    
        manager->connect(manager, &QNetworkAccessManager::finished,
                         [](QNetworkReply *reply) {
            qDebug() << QString("Finished. %1. %2").arg(reply->errorString()).arg(reply->error());
            qDebug() << "readed: " << reply->readAll();
        });
    
        QNetworkReply *reply = manager->get(request);
    
        reply->connect(reply, &QNetworkReply::readyRead, [reply]() {
            qDebug() << QString("readyRead: '%1'").arg(QString(reply->readAll()));
        });
    }
    

    and for server:

        QTcpSocket socket;
        ...
        if(socket.waitForReadyRead(5000))
        {
            QByteArray request;
            request += socket.readAll();
    
            QByteArray responce("HELLO, WORLD! HELLO, WORLD! HELLO, WORLD! HELLO, WORLD!");
    
            socket.write(responce);
            if(!socket.waitForBytesWritten())
            {
                qWarning() << QString("Error occurred in waitForBytesWritten() method of the tcp socket. %1 (%2)")
                              .arg(socket.errorString())
                              .arg(socket.error());
            }
        }
        else
        {
            qWarning() << QString("Error occurred in read method of the tcp socket. %1 (%2)")
                          .arg(socket.errorString())
                          .arg(socket.error());
        }
    

    Also I created a Bug Report

    raven-worxR 1 Reply Last reply
    0
    • I Ildar

      I have simple server with QTcpServer and simple client with QNetworkAccessManager.

      1. When I request data from the server via curl or browser everything is ok
      2. When I request data from any site via QNetworkAccessManager everything is ok
      3. But I can not read data from QTcpServer via QNetworkAccessManager. All requests are reseted. QNetworkAccessManager (client) had send RST (reset connection) right after it received a data from server. You could see the Wireshark logs. And in client code we get the error: "Connection closed" (RemoteHostClosedError)
      4. Aslo, I tried use QNetworkAccessManager from DownloadManager example and QTcpServer from FortuneServer example in various combinations, but the results were the same.

      Tested Qt Versions:

      • Mac Qt 5.7
      • Linux Qt 5.7
      • Linux Qt 5.6.2
      • Linux Qt 5.5.1

      Wireshark logs:
      https://drive.google.com/file/d/0B0Pb09eHyZTfUThQNTctQVJOUVU/view?usp=sharing

      Wireshark screenshot: qt-wireshark.png
      The upper parts (with red lines) are results of QNetworkAccessManager, and the latest packets with success result are curl attempt to get data from QTcpServer

      Also there is a simple example to reproduce the error: testNetwork.zip

      And here is sample code for client:

      void test(quint16 port)
      {
          QNetworkAccessManager *manager = new QNetworkAccessManager();
      
          QNetworkRequest request;
          request.setUrl(QUrl(QString("http://127.0.0.1:%1/").arg(port)));
      
          manager->connect(manager, &QNetworkAccessManager::finished,
                           [](QNetworkReply *reply) {
              qDebug() << QString("Finished. %1. %2").arg(reply->errorString()).arg(reply->error());
              qDebug() << "readed: " << reply->readAll();
          });
      
          QNetworkReply *reply = manager->get(request);
      
          reply->connect(reply, &QNetworkReply::readyRead, [reply]() {
              qDebug() << QString("readyRead: '%1'").arg(QString(reply->readAll()));
          });
      }
      

      and for server:

          QTcpSocket socket;
          ...
          if(socket.waitForReadyRead(5000))
          {
              QByteArray request;
              request += socket.readAll();
      
              QByteArray responce("HELLO, WORLD! HELLO, WORLD! HELLO, WORLD! HELLO, WORLD!");
      
              socket.write(responce);
              if(!socket.waitForBytesWritten())
              {
                  qWarning() << QString("Error occurred in waitForBytesWritten() method of the tcp socket. %1 (%2)")
                                .arg(socket.errorString())
                                .arg(socket.error());
              }
          }
          else
          {
              qWarning() << QString("Error occurred in read method of the tcp socket. %1 (%2)")
                            .arg(socket.errorString())
                            .arg(socket.error());
          }
      

      Also I created a Bug Report

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Ildar said in "Connection closed" When using QNetworkAccessManager and QTcpServer:

      Also I created a Bug Report

      This is not a bug!
      Since you are using QNetworkAccessManager you are sending HTTP requests to your server. And QNAM of course expects a HTTP response.

      At least the response should look like the following.

      Note:

      • each line should end with \r\n
      • an empty line separates the header- and content-section
      • Content-Length header is the byte-count of the content
      HTTP/1.1 200 OK
      Date: Thu, 20 Oct 2016 12:28:53 GMT
      Server: MyServer 1.0.0
      Content-Length: 3
      Content-Type: text/plain
      Connection: Closed
      
      XXX
      

      You are simply sending the content directly. So no status line (HTTP/1.1 200 OK) to indicate the result, thus QNAM closes the connection due to an unexpected response.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      I 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @Ildar said in "Connection closed" When using QNetworkAccessManager and QTcpServer:

        Also I created a Bug Report

        This is not a bug!
        Since you are using QNetworkAccessManager you are sending HTTP requests to your server. And QNAM of course expects a HTTP response.

        At least the response should look like the following.

        Note:

        • each line should end with \r\n
        • an empty line separates the header- and content-section
        • Content-Length header is the byte-count of the content
        HTTP/1.1 200 OK
        Date: Thu, 20 Oct 2016 12:28:53 GMT
        Server: MyServer 1.0.0
        Content-Length: 3
        Content-Type: text/plain
        Connection: Closed
        
        XXX
        

        You are simply sending the content directly. So no status line (HTTP/1.1 200 OK) to indicate the result, thus QNAM closes the connection due to an unexpected response.

        I Offline
        I Offline
        Ildar
        wrote on last edited by
        #3

        @raven-worx Thank you very much for your response!

        1 Reply Last reply
        0

        • Login

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