I don't know. Do you mean:
but it works...
I don't know. Do you mean:
but it works...
No, then bufferring wouldn't make any sense. What is the purpose of the 'forever' loop?
That's logic. The buffer has to b cleared after processing record.
forever loop is a infinite loop.
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.
can you explain the thing, please.
I want to understand it.
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.
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.
@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.
Bookmarks