Results 1 to 7 of 7

Thread: QDataStream extra bytes in output?

  1. #1
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDataStream extra bytes in output?

    I have created a small TCP server application and attached it to this message.

    Notice in server_thread.cpp, there are two "BOOKMARK" comments.

    The user connects and types in some text, the server is supposed to echo it back to them preceded by two less-than symbols and one space ("<< ").

    Notice that the number of bytes to write is ALWAYS four bytes more than what was read from the user (there's a print statement near both BOOKMARKs). Where are these four extra bytes coming from?

    I've even tried taking out the "<< ", but that's not where the extra bytes are coming from.

    Any suggestions would be appreciated.

    Sincerely,

    Gordon E.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream extra bytes in output?

    QDataStream is a serialization mechanism, not a general binary stream - it adds some additional data that allows deserialization of the stream later on. In your case the additional four bytes probably contain the length of the string you redirect into the stream.

  3. The following user says thank you to wysota for this useful post:

    grellsworth (15th November 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream extra bytes in output?

    Quote Originally Posted by grellsworth View Post
    Where are these four extra bytes coming from?
    It's the version number.

    http://doc.trolltech.com/4.3/qdatast...tml#versioning
    http://doc.trolltech.com/4.3/datastreamformat.html

  5. The following user says thank you to jacek for this useful post:

    grellsworth (15th November 2007)

  6. #4
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream extra bytes in output?

    I've ruled out the extra bytes being the version, as they change depending on how much the user types in. So I'm thinking that that it probably is the length.

    However, I've re-written my program in two parts, a server and a client... and I've followed the examples given in 'threaded fortune server' and 'fortune client', but I'm still getting extra "junk" somewhere.

    When I send data from the client to the server, I send the number of bytes being sent as a serialized quint16 (as in fortune server), and I extract that number on the server side and test it against bytesAvailable() (as in fortune client). Then when the server sends back to the client, I follow the same step, I send the number of bytes to be sent as a serialized quint16.

    Somehow I'm still getting "junk" somewhere.

    I've attached my server and client code in .zip format.
    Attached Files Attached Files

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream extra bytes in output?

    Get rid of the data stream and write data to the socket directly using QIODevice::write.

  8. The following user says thank you to wysota for this useful post:

    grellsworth (15th November 2007)

  9. #6
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream extra bytes in output?

    OK, I've fixed it without getting rid of the data stream.

    I was doing this before (for some reason this injects garbage out):

    This is in the server code...
    Qt Code:
    1. outDataStream << (quint16)0;
    2. outDataStream << "<< ";
    3. outDataStream << output;
    4. outDataStream << (quint16)(outByteArray.size() - sizeof(quint16));
    To copy to clipboard, switch view to plain text mode 

    And now I'm doing this, instead (this works fine):
    Qt Code:
    1. outDataStream << (quint16)0;
    2. outDataStream << "<< " + output;
    3. outDataStream << (quint16)(outByteArray.size() - sizeof(quint16));
    To copy to clipboard, switch view to plain text mode 

    I'm not sure what the real difference is, but the second one works flawlessly.

    Thanks for all your help.

    Gordon E.

  10. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream extra bytes in output?

    The difference is that in the first case you send two strings, not one, and each of them is serialized separately.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.