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. Howto neglect these bytes while downloading file from http server
Forum Updated to NodeBB v4.3 + New Features

Howto neglect these bytes while downloading file from http server

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 686 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.
  • C Offline
    C Offline
    ChrisW67
    wrote on last edited by
    #3

    The four bytes look like the number of bytes in the remainder of the message.

    1 Reply Last reply
    1
    • P Offline
      P Offline
      pingal
      wrote on last edited by
      #4

      ChrisW67: You are right. That's the data length. The problem was in the below lines:

      QDataStream out(&file);
      out << bytes;
      

      When bytes are written to QDataStream in this fashion, Its write the content length followed by the actual data. (I thought it will just write the content).

      So i figured out to use QDataStream::writeRawData

      QDataStream out(&file);
       size_t size = bytes.size();
       out.writeRawData(bytes, size);
       file.close();
      
      C 1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #5

        Why do you need a QDataStream then at all?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • P Offline
          P Offline
          pingal
          wrote on last edited by
          #6

          I don't actually need it if i just use it for downloading a file, In fact, it is easier to just use QFile::write(..) in the above case. I though it would be easier to prefix/suffix files content like below:

          out << prefix;
          out << file;
          out << suffix

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #7

            If you use QDataStream on the sender side you must also use it on the receiver side. Everything else will break sooner or later.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • P Offline
              P Offline
              pingal
              wrote on last edited by
              #8

              @Christian-Ehrlicher : I didn't notice that, can you please elaborate it a little.

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #9

                Don't know what else I should say - you use QDataStream to create your stream so you have to use QDataStream to deserialize the data later on. QDataStream adds data to the stream (as you already noticed) and may alter your data in any way: "A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris."
                So reading it as raw data may or may not work. The format of QDataStream's binary representation is not documented so you it can change.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  pingal
                  wrote on last edited by
                  #10

                  oky. thank you :)

                  1 Reply Last reply
                  0
                  • P pingal

                    ChrisW67: You are right. That's the data length. The problem was in the below lines:

                    QDataStream out(&file);
                    out << bytes;
                    

                    When bytes are written to QDataStream in this fashion, Its write the content length followed by the actual data. (I thought it will just write the content).

                    So i figured out to use QDataStream::writeRawData

                    QDataStream out(&file);
                     size_t size = bytes.size();
                     out.writeRawData(bytes, size);
                     file.close();
                    
                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #11

                    @pingal I missed that in your original post. I assumed you were receiving extra bytes, which is not the case.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pingal
                      wrote on last edited by
                      #12

                      @ChrisW67 Yes, that's what i thought at first but when I STDOUT QByteArray, then i realized that serializing data to stream is causing those extra bytes.

                      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