Results 1 to 8 of 8

Thread: how to peek at serialized data from QDataStream

  1. #1
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to peek at serialized data from QDataStream

    Hello,

    I am adapting networking code from Fortune client and Fortune server examples in Qt 4.6 documentation for my needs, code for reading data looks like this:
    Qt Code:
    1. QDataStream in(tcpSocket);
    2. quint16 blockSize;
    3. in.setVersion(QDataStream::Qt_4_0);
    4.  
    5. if (blockSize == 0) {
    6. if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
    7. return;
    8.  
    9. in >> blockSize;
    10. }
    11.  
    12. if (tcpSocket->bytesAvailable() < blockSize)
    13. return;
    14.  
    15. QString nextFortune;
    16. in >> nextFortune;
    To copy to clipboard, switch view to plain text mode 

    sending data on the server like this:
    Qt Code:
    1. QByteArray block;
    2. QDataStream out(&block, QIODevice::WriteOnly);
    3. out.setVersion(QDataStream::Qt_4_0);
    4. out << (quint16)0;
    5. out << fortunes.at(qrand() % fortunes.size());
    6. out.device()->seek(0);
    7. out << (quint16)(block.size() - sizeof(quint16));
    8. ...
    9. clientConnection->write(block);
    To copy to clipboard, switch view to plain text mode 

    it's quite simple and elegant,server attaches quint16 number,size of the data at the beginnig of each network message,then client reads it only when all of the data is available. however,it first reads the block size which removes it from the socket's buffer and i would like to just peek at the block size and leave it in the buffer.
    My problem is,using QTcpSocket.peek() gives me access to the raw data,not to the serialized quint16 blockSize at the begining of the received data buffer. So,is there any proper way to peek at serialized data from QDataStream? Or is it a good idea to insert block size at the server side as raw data?
    I could live without it,but it would somewhat complicate my code.
    Thanks in advance for all answers.

  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: how to peek at serialized data from QDataStream

    Why do you want to leave the block size in the buffer? Read it and store it in a variable so that you can reuse the value when needed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to peek at serialized data from QDataStream

    Quote Originally Posted by wysota View Post
    Why do you want to leave the block size in the buffer? Read it and store it in a variable so that you can reuse the value when needed.
    well,on the server,that would require storing the block size value for every connected socket to a client and i wanted to avoid that so i would have simpler code..but other requirements force me to complicate things anyway,so it won't be a problem now..still it would be nice to be able to peek at serialized data. of course it wouldn't be a problem on a client with single socket to server..i might have not formulated my needs properly (*embarrased*)

  4. #4
    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: how to peek at serialized data from QDataStream

    Quote Originally Posted by jmenda View Post
    well,on the server,that would require storing the block size value for every connected socket to a client
    I'm sure that's the least of your problems. I'd worry more about exposing your server to DOS attacks which is what you already do.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to peek at serialized data from QDataStream

    By sending some very large value as block size? Or is there some other problem as well? It's just skeleton example code but I'll appreciate any thoughts about it's limitiations or errors.

  6. #6
    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: how to peek at serialized data from QDataStream

    Quote Originally Posted by jmenda View Post
    By sending some very large value as block size?
    Yes, that's one of the problems. Receiving bogus data in the block itself is another problem. QDataStream as a network protocol is much over advertised.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to peek at serialized data from QDataStream

    Receiving bogus data in the block itself is another problem. QDataStream as a network protocol is much over advertised.
    Checking for validity of correct type of data is my application's problem. And I understand from the documentation,that corrupted data should change QDataStream's status to QDataStream::ReadCorruptData.. so what other problem there could be? I assume possibility of buffer overflow,etc. is taken care of by qt developers.

  8. #8
    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: how to peek at serialized data from QDataStream

    Quote Originally Posted by jmenda View Post
    Checking for validity of correct type of data is my application's problem. And I understand from the documentation,that corrupted data should change QDataStream's status to QDataStream::ReadCorruptData.. so what other problem there could be?
    That you lose sync with the stream and the only thing you can do is disconnect the client. Besides, nobody checks the status of the stream anyway.

    I assume possibility of buffer overflow,etc. is taken care of by qt developers.
    There is no buffer overflow. Clients can just crash your machine by feeding it more data that the machine has RAM available while often you wait just for a few bytes of data. It is a rare case that your server works on generic data, usually you have some kind of protocol and you know how much data to expect. Relying on the client to provide a size followed by a blob is just asking for trouble.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    jmenda (24th May 2011)

Similar Threads

  1. Serialized QByteArray length
    By giantdragon in forum Qt Programming
    Replies: 4
    Last Post: 21st May 2011, 22:10
  2. QDataStream loses data over QTcpSocket?
    By KShots in forum Qt Programming
    Replies: 11
    Last Post: 18th March 2011, 22:05
  3. Qt QDataStream supports data types
    By LoginFailed in forum Qt Programming
    Replies: 2
    Last Post: 30th December 2009, 08:41
  4. How to peek at the Event queue?
    By TheRonin in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2009, 14:21
  5. avaiability of data from QDataStream
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 19th June 2006, 12:32

Tags for this Thread

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.