The sticky phenomenon of TCP

Original link: http://www.cnblogs.com/qiaoconglovelife/p/5733247.html

When I was watching the interview, I saw that an interviewer asked about the sticky packet of TCP. When I think of the problem of sticky packets when I do a shopping cart to process data update, let’s summarize it.

1 What is sticky bag phenomenon

  TCP sticky packet means that several packets of data sent by the sender are glued into one packet when received by the receiver. From the receiving buffer, the head of the next packet of data is immediately followed by the end of the previous packet of data.

2 Why does the sticking phenomenon occur?

  (1) The reason of the sender

  We know that TCP uses the Nagle algorithm by default. The Nagle algorithm mainly does two things: 1) The next packet is sent only when the previous packet is confirmed; 2) Multiple small packets are collected and sent together when an acknowledgment arrives.

  Therefore, it is Nagle's algorithm that causes the sender to cause sticky packets.

  (2) The reason of the recipient

  When TCP receives a packet, it does not immediately send it to the application layer for processing, or in other words, the application layer does not necessarily process it immediately; in fact, TCP saves the received packet into the receive buffer, and then the application actively removes the packet from the buffer. Read received packets. In this way, if TCP receives packets faster than the application reads packets, multiple packets will be stored in the cache, and when the application reads, it will read multiple packets that are glued together end to end.

3 When do you need to deal with sticky bags?

  (1) If the multiple packets sent by the sender are originally different parts of the same data, for example, a large file is divided into multiple packets and sent, then, of course, there is no need to deal with the phenomenon of sticky packets;

  (2) But if multiple groups are irrelevant, or even in a parallel relationship, we must deal with the problem of sticky packages. For example, each packet I wanted to receive at that time was a product information with a fixed format. If I didn’t deal with the sticky packet problem, I would only process the frontmost product for each incoming packet, and the latter ones would be discarded. This is obviously not the result I want.

4 How to deal with sticky bags

  (1) sender

  For the sticky packet phenomenon caused by the sender, we can solve it by turning off the Nagle algorithm, and use the TCP_NODELAY option to turn off the Nagle algorithm.

  (2) Recipient

  Unfortunately, TCP does not have a mechanism to deal with the receiver's sticky packet phenomenon, we can only deal with it at the application layer.

  (3) Application layer processing

  Application layer processing is simple and easy! And it can not only solve the sticky packet problem caused by the receiver, but also solve the sticky packet problem caused by the sender.

  The solution is loop processing: when the application processes the packets read from the cache, after reading a piece of data, it should loop to read the next piece of data until all the data are processed; but how to judge the length of each piece of data?

  Two ways:

    1) Formatting data: Each piece of data has a fixed format (starting character, ending character), this method is simple and easy to implement, but when choosing the starting character and ending character, you must pay attention to the inside of each data. The start character must not appear or terminator;

    2) Sending length: When sending each piece of data, send the length of the data together. For example, you can choose the first 4 bits of each piece of data to be the length of the data. When processing the application layer, you can judge the start and end of each piece of data according to the length. end.

  When I was making a shopping cart, my initial approach was to set the start character (0x7e) and the end character (0xe7), but when testing a large amount of data, I found that the data was abnormal. As I guessed, it was discovered during debugging that some data had them inside. Because the amount of data to be processed is very large, in order to be foolproof, I finally adopted the method of sending the length. Never had a problem with sticky bags again.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731767&siteId=291194637