tcp sticky packet problem

Recap: 

  Only TCP has sticky packets, UDP never sticks packets

 

 Knowledge reserve:

        The principle of socket sending and receiving messages

  

The sender can send data in one K to K, and the application program on the receiving end can take away the data in two K and two K. Of course, it is also possible to take away 3K or 6K data at a time, or only take out a few bytes of data at a time. That is to say, the data seen by the application is a whole, or a stream, and how many bytes of a message are invisible to the application, so the TCP protocol is a stream-oriented protocol, which is also easy The reason for the sticky package problem. UDP is a message-oriented protocol, each UDP segment is a message, and the application must extract data in units of messages, and cannot extract any byte of data at a time, which is very different from TCP. How to define a message? It can be considered that the data that the other party writes/sends at one time is a message. What needs to be understood is that when the other party sends a message, no matter how the bottom layer is segmented, the TCP protocol layer will sort the data segments that make up the entire message. Rendered in the kernel buffer.

For example, a tcp-based socket client uploads a file to the server. When sending, the content of the file is sent according to a byte stream. When the receiver sees it, he does not know where the byte stream of the file starts. where it ends

The so-called sticky packet problem is mainly caused by the fact that the receiver does not know the boundaries between messages and does not know how many bytes of data to extract at one time.

In addition, the sticky packet caused by the sender is caused by the TCP protocol itself. In order to improve the transmission efficiency of TCP, the sender often needs to collect enough data before sending a TCP segment. If the data that needs to be sent several times in a row is very small, usually TCP will combine the data into a TCP segment according to the optimization algorithm and send it out at one time, so that the receiver receives the sticky packet data.

  1. TCP (transport control protocol, transmission control protocol) is connection-oriented, stream-oriented, and provides high reliability services. There must be a pair of sockets at both ends of the transceiver (client and server). Therefore, the sender uses an optimization method (Nagle algorithm) in order to send multiple packets to the receiver more efficiently. Combine multiple data with small intervals and a small amount of data into a large data block, and then package it. In this way, the receiving end is difficult to distinguish, and a scientific unpacking mechanism must be provided. That is, flow-oriented communication has no message protection boundary.
  2. UDP (user datagram protocol, user datagram protocol) is connectionless, message-oriented, and provides efficient services. The block merge optimization algorithm will not be used. Since UDP supports a one-to-many mode, the skbuff (socket buffer) at the receiving end adopts a chain structure to record each arriving UDP packet. There is a message header (information source address, port, etc.) in the packet, so that it is easy for the receiving end to distinguish and process. That is, message-oriented communication has message protection boundaries.
  3. tcp is based on data streams, so the messages sent and received cannot be empty, which requires adding a processing mechanism for empty messages on both the client and the server to prevent the program from getting stuck, while udp is based on datagrams, even if you input It is empty content (enter directly), it is not an empty message, the udp protocol will encapsulate the message header for you, the experiment is omitted

The recvfrom of udp is blocked. A recvfrom(x) must be sentinto(y) to the only one, and it is completed after receiving x bytes of data. If y>x data is lost, which means that udp will not stick packets at all. But it will lose data, unreliable

The protocol data of tcp will not be lost, and the packet has not been received. The next time it is received, it will continue to receive the last time. The own end always clears the buffer content when it receives the ack. Data is reliable, but sticky packets.

 

Guess you like

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