Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28

Thread: QTcpSocket can't read all bytes

  1. #21
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket can't read all bytes

    I don't know. Do you mean:
    Qt Code:
    1. buffer.clear();
    To copy to clipboard, switch view to plain text mode 
    but it works...

  2. #22
    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: QTcpSocket can't read all bytes

    No, then bufferring wouldn't make any sense. What is the purpose of the 'forever' loop?
    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. #23
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket can't read all bytes

    That's logic. The buffer has to b cleared after processing record.
    forever loop is a infinite loop.

  4. #24
    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: QTcpSocket can't read all bytes

    No, clearing the buffer after processing a record is wrong. What if you get more than one message in one chunk (one call to onReadyRead())? Clearing the buffer will discard everything, including the data you haven't processed.
    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. #25
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket can't read all bytes

    can you explain the thing, please.
    I want to understand it.

  6. #26
    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: QTcpSocket can't read all bytes

    From the beginning: the fact that sender sends "AAA" and then it sends "BBB" doesn't mean that the receiver will receive "AAA" and then "BBB". He can receive "AAABBB" or "AA" and "ABBB" or "AAABB" and "B" or even "A", "A", "A", "B", "B", "B". If your record was three characters long and you received "AAABB" and "B" then after processing "AAABB" and extracting "AAA" from it, you would discard "BB" and when you are called again with the last "B", you would fall out of sync because of the missing two characters. You have two autonomous systems talking to each other and TCP does not know anything such as "record", it just transmits bytes as they flow in. If this explanation is not enough for you then I'm sorry but you'll have to read some book or paper on TCP.
    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. #27
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket can't read all bytes

    Ok I understand how TCP works, but in my case the client sends a request to the server. After that the client waits for response from server.
    All the bytes from server are written into the bytearray until the client notices that the response is complete.
    If the response is completed the client process the response and serializes the data.

    So I can be sure that two responses can't be mixed.

  8. #28
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket can't read all bytes

    @Qiieha
    Even if I designed a client and a server so that they sent 1 message to each other in turns, I would not write the networking code with that assumption in mind. That would work but would be bad practice and not very robust.

    In any case, you need a way for the receiver to know the length of the message it reads, by using a delimiter sequence, prepending the message with its length, using a fixed length, you name it.

Similar Threads

  1. Can somebody show how to read bytes?
    By "BumbleBee" in forum Newbie
    Replies: 36
    Last Post: 20th April 2011, 17:57
  2. Replies: 2
    Last Post: 9th June 2010, 16:08
  3. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 07:38
  4. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 4th July 2007, 23:12
  5. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 20:23

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.