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. QHttpServer and large file
QtWS25 Last Chance

QHttpServer and large file

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 245 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.
  • mrdebugM Offline
    mrdebugM Offline
    mrdebug
    wrote last edited by
    #1

    My QHttpServer server has to transfer a large file.
    With a little file I can use the write() function of QHttpServerResponder object like this

    responder.write(QBABuffer, "application/octet-stream");
    

    where QBABuffer is the intere file.
    But with a large file, how can I send back to the client the file in little chunks?

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    mrdebugM 1 Reply Last reply
    0
    • mrdebugM mrdebug

      My QHttpServer server has to transfer a large file.
      With a little file I can use the write() function of QHttpServerResponder object like this

      responder.write(QBABuffer, "application/octet-stream");
      

      where QBABuffer is the intere file.
      But with a large file, how can I send back to the client the file in little chunks?

      mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote last edited by
      #2

      This code seems to work but there is a problem

              if (File.open(QIODevice::ReadOnly)) {
                  responder.writeBeginChunked("application/octet-stream");
                  while (!File.atEnd()) {
                      QByteArray QBABuffer= File.read(1024);
                      if (QBABuffer.length()) {
                          if (File.atEnd()) responder.writeEndChunked(QBABuffer);
                          else responder.writeChunk(QBABuffer);
                      }
                  }
                  File.close();
              }
      

      The responder.writeChunk(QBABuffer); function is not blocking so the file will be load totally in ram before to send to the client.
      Is there a way to flush the socket?

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kevin Hoang
        wrote last edited by
        #3

        If you want to keep your current logic, you’ll need to add a blocking call like responder.socket()->waitForBytesWritten(-1); right after writeChunk.
        However, I think that's not a very good idea.

        A better way would be to open the file and use the QTcpSocket::bytesWritten event to send data chunk by chunk to the client. It would look something like this:

        QTcpSocket *socket = responder.socket();
        QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket, file, responder = std::move(responder)]() mutable {
            if (file->atEnd()) {
                responder.done();
                file->deleteLater();
                return;
            }
        
            QByteArray chunk = file->read(1024);
            socket->write(chunk);
        });
        
        // Send the first chunk right away
        QByteArray chunk = file->read(1024);
        socket->write(chunk);
        

        This way, you avoid blocking the event loop and can handle multiple clients much more efficiently.

        1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote last edited by
          #4

          Hi, thank you for the suggestion but I haven't got socket() function in QHttpServerResponder object.
          Which Qt version are you using?

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          1
          • K Offline
            K Offline
            Kevin Hoang
            wrote last edited by
            #5

            What I shared was just an idea based on building a custom HTTP server.

            From what I see, Qt currently only offers a very basic HTTP server. If you want better performance, it’s probably best to look at Qt’s source code and create your own server based on it. I haven’t had a chance to test if writeChunk in Qt 6.8 really works as expected, but I have a feeling it might not.

            1 Reply Last reply
            0
            • mrdebugM Offline
              mrdebugM Offline
              mrdebug
              wrote last edited by
              #6

              Solved using QtWebApp
              https://github.com/fffaraz/QtWebApp
              instead of QtHTTPServer

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kevin Hoang
                wrote last edited by
                #7

                Congratulations! Glad to hear you got it resolved. I noticed that QtHttp uses socket->waitForBytesWritten(-1); when writing chunks — it’s a simple and straightforward approach. I’m still curious why QHttp doesn’t apply the same method.

                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