In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to analyze the sticky package, unpacking and solutions of TCP. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Sticking, unpacking and solution of TCP
The problems of sticking and unpacking of TCP often appear in the communication based on TCP protocol, such as RPC framework, Netty and so on. If you have similar techniques on your resume or if the company you are interviewing uses relevant technologies, the chances of being asked about the interview will be very high.
The problems of sticking and unpacking of TCP often appear in the communication based on TCP protocol, such as RPC framework, Netty and so on. If you have similar techniques on your resume or if the company you are interviewing uses relevant technologies, the chances of being asked about the interview will be very high.
Today's article will take you to learn more about the gluing and unpacking of TCP as well as its solutions.
What is a sticky bag?
Before learning sticky bags, correct the pronunciation. In many video tutorials, "gluing" is pronounced as "ni á n". After research, individuals are more inclined to read "zhzhongn bacco".
If you search for "sticky package" on Baidu encyclopedia, the corresponding pronunciation is "zhongnbaoo", which is interpreted as: network technical terminology. In TCP protocol, several packets of data sent by the sender are glued into a packet when received by the receiver. from the receiving buffer, the head of the latter packet is immediately followed by the tail of the previous packet.
TCP is a byte-stream-oriented protocol, which is a string of data without boundaries. There is no concept of "packet". The words "sticky packet" and "unpacking" are intended to help to understand these two phenomena vividly.
Why doesn't UDP have a sticky bag?
The problem of sticky packet unpacking can occur in the data link layer, network layer and transport layer. Most of the daily network application development is carried out in the transport layer. Because UDP has message protection boundary, the problem of sticking and unpacking will not occur, so the problem of sticking and unpacking only occurs in TCP protocol.
The occurrence scenario of sticky package unpacking
Because TCP is stream-oriented and has no boundaries, the operating system optimizes through buffers when sending TCP data, such as a buffer size of 1024 bytes.
If the amount of data sent by a request is relatively small and does not reach the buffer size, TCP will merge multiple requests into the same request to send, which leads to the sticky packet problem.
If the amount of data sent by a request exceeds the buffer size, TCP will split it into multiple transmissions, which is called unpacking.
For sticking and unpacking, you can refer to the following figure:
Gluing / unpacking
The following situations are demonstrated in the figure above:
In a normal and ideal situation, two packets are sent exactly the size of the TCP buffer or the waiting time of the TCP.
Sticky packets: two packets are small and have a short interval, so sticky packets occur and are merged into one packet to send.
Unpacking: a packet is too large, exceeds the cache size, and is split into two or more packets to send
Unpacking and sticking: the Packet1 is too large and the package is unpacked, and some of the removed parts are glued to the Packet2.
Common solutions
There are four common solutions to sticking and unpacking problems:
The sender encapsulates each packet into a fixed length, such as 100 bytes. If less than 100 bytes can be filled to the specified length by filling in 0 or blank, etc.
The sender uses a fixed delimiter at the end of each packet, for example,\ r\ n. If packet unpacking occurs, you need to wait for multiple packets to be sent before finding one of them for merging; for example, FTP protocol
The message is divided into the header and the message body. The length of the entire message is saved in the header. Only after reading a message of sufficient length can a complete message be read.
Carry on the processing of sticking and unpacking through the custom protocol.
How to deal with the problems of sticking and unpacking in Netty
Netty abstracts the solution of sticking and unpacking, and provides some decoders (Decoder) to solve the problem of sticking and unpacking. Such as:
LineBasedFrameDecoder: decoding data packets in units of behavior
DelimiterBasedFrameDecoder: use special symbols as separations to decode packets
FixedLengthFrameDecoder: decoding packets with fixed length
LenghtFieldBasedFrameDecode: for protocols with message headers containing message length (most commonly used)
Based on Netty network reading and writing procedures, you can directly use these Decoder to complete the decoding of data packets. For systems with high concurrency and high traffic, each packet should not transmit excess data (so it is not advisable to make up), and LenghtFieldBasedFrameDecode is more suitable for such scenarios.
The problem of sticking and unpacking of TCP protocol is because the data transmission of TCP protocol is based on byte flow, it does not include the concepts of message and data packet, and requires the application layer protocol to design the message boundary, that is, message frame (Message Framing). If the application layer protocol is not processed based on length or Terminator message boundaries, it will cause multiple messages to be glued and unpacked.
Although there are off-the-shelf solutions in many frameworks, such as Netty, we still need to know the underlying principle, and we also need to know that there is such a thing, in order to better combine the scene to make.
After reading the above, do you have any further understanding of how to analyze the sticky package, unpacking and solution of TCP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.