TCP is a stream of protocols, a complete package may be split by TCP into multiple packets to send, or a small package can be encapsulated into a large packet sent, which is called sticky packet and unpacking problems
The reason why the sticky bag and unpacking occurred:
Occurs in a stream loss, UDP does not appear sticky packets because it has a message boundary
1. The data to be sent is larger than the remaining space in the TCP send buffer and needs to be dismantled
2, the data to be sent is greater than MSS ( maximum message length), TCP will be removed before the transmission of the packet
3, the data to be sent is less than the size of the TCP send buffer, TCP will write the buffer once sent out, there will be sticky packets
4. The application layer receiving the data end does not read the TCP accept buffer data in time, the sticky packet will occur
Adhesive Packaging, Unpacking solutions:
1, message fixed length, such as the size of each message fixed 200 bytes, if not enough, empty space;
2. Add special symbol separators at the end of the package
3, the message is divided into the message header and message body, the message header contains a field representing the total length of the message, usually the design idea is the first field of the message header with an int to represent the total length of the message
4. More complex application layer protocols
Netty has abstracted the above 4 applications and provided 4 decoders:
linebasedframedecoder: Compile the bytebuf in order to see if there is a "\ n" or "\ r \ n", if any, to this position as the end position, The bytes from the readable index to the end of the interval are made up of a line, which is a decoder that ends with a newline character , supports either a carry-terminator or no-terminator decoding, and supports the maximum length of a single line, and throws an exception if no newline character is found after continuous reading to the maximum length. , while ignoring the previously read exception stream
fixedlengthframedecoder: fixed length decoder , which can automatically decode the message according to the specified length, developers do not need to consider the problem of TCP sticky packets, etc. With Fixedlengthframedecoder decoding, regardless of how much data is received at one time, he is decoded according to the length set in the constructor, and if it is a half-packet message, Fixedlengthframedecoder caches the half-packet message and waits for the next packet. After arrival, make a bundle until the full package is read
delimiterbasedframedecoder: Custom delimiter decoding , the first parameter of the constructor represents the maximum length of a single message, and the delimiter is still not found after reaching that length. Throws a toolongframeexception exception to prevent a memory overflow due to a missing delimiter symbol for the exception code stream
Lengthfieldbasedframedecoder: through fixed length to distinguish the whole packet message , message length, the length of the packet size fixed, not enough space to complete, sending and receiving parties follow the same convention, This makes it possible to differentiate the fixed-length message even if it is wrapped by a receiver program.
·
Netty decoder and sticky pack unpacking