When I saw the sticky package and unpack is actually a little confused, this is the beginning of my learning for Baidu. So I found an interesting point, which is the quotation marks I put on the title, which is recorded here today
What is the package
When I searched, I found that there are two kinds of bags
- One is data at the application layer. The data sent by the application layer through the Send method is called application-layer packets
- Data at the application layer will be encapsulated by the transport layer. The packet head and packet data together form a packet
Glue bag phenomenon
There are also two scenarios
- One is that packets of multiple transport layers are read by the same buffer. That is, the sender sends consecutive packets of multiple application layers, but the receiver reads all packets only once.
- In fact, this is only a packet boundary problem, TCP clearly stated. It streams your data to each other in an error-free sequence, but requires the application to parse the stream itself (stream2datagram).
- To solve
- Method 1 writes a special partition character to the data boundary
- Method 2 writes the length of the message in the header, which is actually what IP and TCP do
- Method 3 Indicates the length of a fixed message
- Second, it refers to the implementation of TCP. In order to solve the problem that the packet header ratio under the scenario of a large number of tabloid articles is large and the transmission cost performance is too low, Nagle algorithm is specially designed. Nagle’s algorithm typically adds two lines of code to the TCP program, telling the sender to send data to the cache if it is not confirmed to be sent. Any data is then continued until significant data confirmation is obtained or until a certain amount of data has been accumulated
- This can be solved by turning off the Nagle algorithm
- The rules of Nagle (see tcp_output.c for tcp_nagle_check) :
- (1) If the packet length reaches MSS, it is allowed to send;
- (2) If the packet contains FIN, it is allowed to send;
- (3) if the TCP_NODELAY option is set, sending is allowed;
- (4) If the TCP_CORK option is not set, if all sent small packets (packet length less than MSS) are confirmed, the packet is allowed to be sent;
- (5) If none of the above conditions is met, but a timeout (generally 200ms) occurs, it will be sent immediately.
Unpacking phenomenon
- One is that the written data is larger than the size of the receiver buffer, and the application layer needs to read the data multiple times
- MSS (Maximum packet Length) is segmented. When the length of the TCP packet -TCP header is greater than MSS, packet disassembly occurs