In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 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 TCP sticking, unpacking and what the solution is. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.
TCP packet sticking and unpacking problems often occur in TCP based communications, such as RPC framework, Netty and so on. If you include similar techniques in your resume or if the company you're interviewing for uses them, the chances of being asked about the interview are high.
Today, I will take you to learn more about TCP sticking packets and unpacking and solutions.
What's a sticky bag?
Before learning how to stick bags, correct the pronunciation. Many video tutorials pronounce "sticky" as "nián." After investigation, individuals are more inclined to read "zhān bāo."
If you search for "sticky package" on Baidu Encyclopedia, the corresponding pronunciation is "zhān bāo," which is interpreted semantically as: network technical term. In TCP protocol, several packets of data sent by the sender are pasted into a packet when received by the receiver. From the receiving buffer, the head of the next packet of data immediately follows the tail of the previous packet of data.
TCP is a protocol oriented to byte stream, that is, a string of data without boundaries. There is no concept of "packet" originally."Sticking packet" and "unpacking packet" are said to help understand these two phenomena vividly.
Why does UDP have no sticky packets?
Sticky packet unpacking problems can occur at the data link layer, network layer, and transport layer. Daily network application development is mostly carried out in the transport layer, because UDP has message protection boundaries, there will be no sticky packet unpacking problem, so sticky packet unpacking only occurs in TCP protocol.
Sticky package unpacking scene
Because TCP is stream-oriented and has no boundaries, the operating system optimizes TCP data by sending buffers, such as 1024 bytes in size.
If the amount of data sent in a request is relatively small and does not reach the buffer size, TCP will combine multiple requests into the same request for transmission, which forms a sticky packet problem.
If the amount of data sent in one request is large and exceeds the buffer size, TCP will split it into multiple transmissions, which is unpacking.
For gluing and unpacking, please refer to the following figures:
The following scenarios are illustrated in the figure above:
In the normal ideal case, two packets just meet the size of TCP buffer or reach TCP waiting time, and two packets are sent respectively.
Sticky packets: two packets are smaller, the interval is short, sticky packets occur, combined into a packet sent;
Unpacking: A packet is too large to exceed the buffer size, split into two or more packets to send;
Unpacking and sticking: Packet1 is too large and unpacked, and part of it is stuck with Packet2.
common solution
There are four common solutions to sticking and unpacking problems:
The sender encapsulates each packet into a fixed length, such as 100 bytes. If it is less than 100 bytes, it can be filled to the specified length by filling 0 or empty.
The sender uses a fixed delimiter at the end of each packet, such as\r\n. If unpacking occurs, wait for multiple packets to be sent before finding\r\n of them for merging; for example, FTP protocol;
Divide a message into a header and a message body. The header stores the length of the entire message. Only after reading a message of sufficient length can a complete message be read.
Stick and unpack through custom protocols.
Netty's treatment of sticking and unpacking
Netty abstracts the solution to packet sticking and unpacking, and provides some decoders to solve the problem of packet sticking and unpacking. For example:
LineBasedFrameDecoder: decodes packets in line units;
DelimiterBasedFrameDecoder: decode packets with special symbols as separators;
FixedLengthFrameDecoder: decodes packets with a fixed length;
LenghtFieldBasedFrameDecode: for protocols where the message header contains the message length (most commonly used);
Netty-based network read-write programs can directly use these Decoders to complete the decoding of packets. For systems with high concurrency and large traffic, each packet should not transmit redundant data (so it is not advisable to supplement it). LenghtFieldBasedFrameDecode is more suitable for such scenarios.
TCP packet sticking and unpacking problem is because TCP protocol data transmission is based on byte stream, it does not contain the concept of message, data packet, etc., the application layer protocol needs to design the boundary of the message itself, that is, message framing. If the application layer protocol does not use length-based or termination-based processing, it can result in multiple message pasting and unpacking.
Although there are ready-made solutions in many frameworks, such as Netty, we still need to be clear about the underlying principles, and we also need to know that there are such things in order to better use them in combination with scenarios.
After reading the above, do you have any further understanding of TCP sticking, unpacking and what the solution is? If you still want to know more knowledge or related content, please pay attention to 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.