Problem arises
A complete business may be split into multiple packets by TCP to send, it is also possible to encapsulate a number of small packets into a large packet sent, this is the TCP unpacking and packet problem.
Here's a picture of a client sending a package to the server:
1. In the first case, both DATA1 and DATA2 are sent separately to the server side, without the case of sticky packets and unpacking.
2. In the second case, the DATA1 and Data2 data are glued together, and a large packet is sent to the server side, which is the case of sticky packets.
3. In the third case, Data2 was separated into data2_1 and data2_2, and Data2_1 reached the server before Data1, which resulted in the unpacking.
Because of the complexity of the network, it is possible that data will be separated into more than n complex unpacking/gluing cases, so it is necessary to solve the problem of unpacking/sticking first when doing TCP server.
Causes of TCP sticky packets and unpacking
1. Application writes data with a larger byte size than the socket send buffer
2. Make the MSS-sized TCP segment. MSS is the abbreviation of the maximum message segment length. MSS is the maximum length of a data field in a TCP message segment. The data field plus the TCP header is equal to the entire TCP message segment. So MSS is not the maximum length of the TCP segment, but rather: Mss=tcp message segment length-tcp header length
3. Ethernet payload is larger than the MTU for IP sharding. MTU refers to the maximum packet size that can be passed on a layer of a communication protocol. If the IP layer has a packet to be transmitted, and the length of the data than the link layer MTU, then the IP layer will be fragmented, the packet is divided into dry slices, so that each piece does not exceed the MTU. Note that IP shards can occur on the original send-side host or on an intermediate router.
Solution strategies for TCP sticky packets and unpacking
1. The message is set long. For example, 100 bytes.
2. Add a carriage return at the end of the package or a special character such as space characters, typically such as FTP protocol
3. Divide the message into the message header and the end of the message.
4. Other complex protocols, such as RTMP protocol.
About the sticky and unpacking of TCP