Results 1 to 9 of 9

Thread: QDataStream and readBytes

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QDataStream and readBytes

    Hi,

    In the wake of this thread thread I'm trying to read binary data from a socket, hence the code:
    Qt Code:
    1. unsigned int totalBytes = 0;
    2. totalBytes = tcpSocket.bytesAvailable();
    3. if (totalBytes < 4) return; // bogus tx...
    4.  
    5. uint headerSize=sizeof(uint);
    6. char * header = new char [headerSize];
    7.  
    8. uint headerBytes=0;
    9. QDataStream inData(&tcpSocket);
    10.  
    11. inData.readBytes(header, headerSize);
    12. headerBytes=atoi(header);
    To copy to clipboard, switch view to plain text mode 

    Unless I really don't know what I'm doing... (most probably) why does inData.readBytes(header, headerSize); always cause a segfault?

    Any pointers welcome.
    Thanks in advance,
    Pedro Doria Meunier.

  2. #2
    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 and readBytes

    Use QDataStream::readRawData(). readBytes() is a complement of writeBytes() and it assumes that the data is in certain format (it also allocates the buffer itself).

    Won't it be simplier to write:
    Qt Code:
    1. uint headerBytes=0;
    2. QDataStream inData(&tcpSocket);
    3. inData >> headerBytes;
    To copy to clipboard, switch view to plain text mode 
    ?

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

    pdoria (18th January 2008)

  4. #3
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Re: QDataStream and readBytes

    Txs Jacek,

    I already tried that only to discover that these devices are stupid enough to send the header and data all at once, so in >> is not an option. I have to read the 1st bytes of what comes to determine the length of the following data...

    Kind regards,
    Pedro Doria Meunier.

  5. #4
    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 and readBytes

    Quote Originally Posted by pdoria View Post
    I already tried that only to discover that these devices are stupid enough to send the header and data all at once, so in >> is not an option.
    Why?

    Qt Code:
    1. quint16 header;
    2. inData >> header;
    To copy to clipboard, switch view to plain text mode 
    will read exactly two bytes from the stream into header, leaving the rest intact. Then you can check the header and read more data.

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

    pdoria (18th January 2008)

  7. #5
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: QDataStream and readBytes

    Jacek,

    You're absolutely right. This is working now. I don't remember what I did last time...
    Man... what a ride!!!
    I sincerely want to thank you for your valuable support... thank you!

    Kind regards,
    Pedro Doria Meunier.

  8. #6
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Arrow Re: QDataStream and readBytes

    Ok. last step:

    this doesn't work...

    Qt Code:
    1. quint16 dataLength=0;
    2.  
    3. QDataStream inData(&tcpSocket);
    4.  
    5. while (zerocount < 4 && dataLength==0)
    6. {
    7. inData >> dataLength;
    8. if (dataLength==0) zerocount++;
    9. }
    10.  
    11. // exceed number of tries... bogus txs. return.
    12. if (zerocount==3) return;
    13.  
    14. char * data = new char[dataLength];
    15. inData >> data;
    To copy to clipboard, switch view to plain text mode 

    the data is always 0x0 ...
    So ... please tell me... after in >> dataLength the pointer in the stream is advanced or not?
    the operator>> ( char *& s ) is able to convert the binary data into char?

    Thanks in advance.
    Last edited by jpn; 18th January 2008 at 20:33. Reason: missing [code] tags

  9. #7
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream and readBytes

    bump.

    Must one do this?

    Qt Code:
    1. char * data = new char[dataLength];
    2. qint8 c;
    3. int n;
    4. for (n=0; n<dataLength; n++) {
    5. inData >> c;
    6. data[n]=c;
    7. }
    To copy to clipboard, switch view to plain text mode 

    It feels a bit archaic...

  10. #8
    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 and readBytes

    Quote Originally Posted by pdoria View Post
    Must one do this?
    No, you can use readRawData() instead.

  11. #9
    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 and readBytes

    Be aware that QDataStream is not a general purpose binary stream but a serialization method. Don't use it if you don't need it.

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.