Results 1 to 2 of 2

Thread: How to read only a certain amount of bytes

  1. #1
    Join Date
    Feb 2006
    Qt products
    Unix/X11 Windows

    Question How to read only a certain amount of bytes

    I'm looking at the code of qt example "the fortune client", and they use the code

    Qt Code:
    1. QString nextFortune;
    2. in >> nextFortune;
    To copy to clipboard, switch view to plain text mode 
    to read a fortune from the socket, but what if they had got more data than that?
    If the server sent two fortunes and the fortunes were broken down into little pieces and then received in some order, would the socket re-arrange the data so I get first one fortune and then the other one? I guess I'm a little clueless on network programming. I would assume that the networkstack is clever enough to put the pieces together in the right order before I read it from my Qt program, is it not?

    But assuming that I only want to read the number of bytes that are given in the variable blocksize in the fortune client, how would I do that, even if there are more data waiting to be read? Assuming the fortune is stored as a Qstring (16 bit per char isn'it?) and then I really want to read data and put it back as a QString.

  2. #2
    Join Date
    Dec 2006
    Thanked 162 Times in 151 Posts
    Qt products

    Default Re: How to read only a certain amount of bytes

    if you want to read a limited number of bytes: see QIODevice::read(qint64) (note that you might get less than that!)

    If you write a QString to a QDataStream than reading it back in from a QDataStream (assuming that the you did not short read the package) will read just that string. The bytes behind will not become part of the string.
    (As you don't (want to) know how Qt writes a QString to a QDataStream, reading a limited number of bytes does not help you here, btw)

    On the other hand, the fortune server from the examples sends just one fortune and then disconnects. So the client does not have to worry about excess data.


Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 14:51
  2. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 10:16
  3. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 21:23
  4. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15:12
  5. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 01:29


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.