Https://www.v2ex.com/t/234785#reply3
1.
This is the problem with IO for byte stream. The TCP protocol in the socket is a stream-oriented protocol, and the sender and receiver are not one by one corresponding. So the so-called sticky packet phenomenon. How to deal with it? Method 1: The protocol package is fixed long. The length of each packet sent out is fixed. For example, they are all 10 bytes. Collect 10 bytes Each time, when a complete packet. Method 2: Tell the package the length of the protocol header to start a fixed-length byte to inform the subsequent package length. Receiver pays first packet long bytes, know that the subsequent packet length and then collect. Method 3: Identify the end of the package with an end flag that never appears in the package body.
2.
My way of dealing with data based on the protocol header and protocol end to determine the message, so that is sticky packet, it is OK, when you receive the protocol end to determine the integrity of the entire message. Sticky packet is because the size of the sending buffer is larger than the data that needs to be sent, the data waits to reach the buffer size, the data is sent to the sticky packet, so in order to avoid sticky packets, after you send the data, empty buffer, you should be able to solve, but it is best to study TCP, this can avoid many problems.
Socket sticky packet problem (GO)