When writing an application using the TCP protocol, you need to consider a problem: The TCP protocol is no message boundary, that is, data from a single send method cannot be guaranteed to be read by a single receive method.
eg
First send: ABCDEFG second send: 123456 when the receiving party receives the data, the following conditions may occur:
First time receive: Abcdefg123456 may also appear: First receive: ABC second receive: Efg12 third time: 3456
Therefore, to resolve the TCP Send message and receive message consistency, programming must resolve the message boundary problem.
Workaround: 1, send a fixed-length message. This kind of square is suitable for the information length fixed occasion.
2. Send the message length with the message. This method increases the amount of data transmitted and increases the amount of programming effort.
Eg: the length of this information is indicated by 2 bytes in front of each sent string message, and the receiver obtains the length of the information from the two bytes first. The string that is sent by the sender is then looped according to the length value
3. Separate messages with special tags. This method is suitable for situations where the information itself does not contain special tags. If you send a line of information, you can use carriage return as a delimiter.
No message boundary problem for "Go" TCP protocol