The difference between TCP and Udp

TCP:

TCP is a connection-oriented and reliable protocol. When communicating with each other based on TCP, the two parties need to establish a TCP connection. The three-way handshake is needed to establish the connection

1, based on connection, high reliability

2. There is a connection process (three-time handshake process), with delay and poor real-time performance.

3. When transmitting the same data, the TCP header costs 20 bytes; The UDP header has a small overhead of only 8 bytes. The TCP header is more complex than UDP, so it contains less user data. TCP does not lose packets, but UDP does. Therefore, THE cost of TCP is large, while the cost of UDP is small.

4. Each TCP connection can only be point-to-point. UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactions

Udp:

Is a connectionless and unreliable protocol, in the process of communication, it does not need to establish a connection first like TCP, as long as (destination address, port number, source address, port number) identified, can send information directly to the message, and do not need to make sure that the server must be able to receive or receive the complete data. It only provides the checksum mechanism to ensure whether a packet is complete. If the checksum fails, the packet is discarded without any processing.

Application scenario:

Protocol Comparison:

TCP adhesive package, subcontract solution

Reasons for sticking:

(1) The sticky packet caused by the sender is caused by THE TCP protocol itself. To improve the transmission efficiency of TCP, the sender usually collects enough data before sending a packet of data. If the data sent for several consecutive times is small, TCP usually combines the data into a packet based on the optimized algorithm and sends the packet at a time. In this way, the receiver receives the sticky packet data.

(2) The sticky packets caused by the receiver are caused by the user process of the receiver not receiving data in time, which leads to the sticky packets phenomenon. This is because the receiver to receive the data in the receive buffer system, the user process data from the buffer if disposing data before arrival of the data has not been removed by the user process, is a packet of data on the system after receive buffer before received a packet of data, and the user process according to the preset buffer size to fetch the data from the receive buffer system, So you get more than one packet at a time.

Solution of sticking package ———— Package:

To packet is to add a header to a piece of data, so that the packet is divided into two parts: the header and the body. The header is actually a struct of a fixed size. There is a struct member variable that represents the length of the package, which is an important variable. Other struct members can be defined as needed. According to the fixed length of the packet header and the variable containing the length of the packet body in the packet header, a complete packet can be split correctly.

Unpacking: Resolve each complete packet according to the packet header rules, and then perform corresponding service processing.

An article on packaging and unpacking

UDP subcontract sender and receiver reassemble packets

Subcontract: subcontract sending (the header of package, including package size, type, serial number, quantity, etc.)

1, in the client you want to send content (files or whatever) into blocks, each block of content is numbered, and then sent;

2. After receiving your data block, the server will reassemble it according to the number of your client data class.

3. Generally, when we send data, we try to use a relatively small data block (mine is not more than 1024). If the data block is too large, it is easy to send and receive data for a long time, and the matching problem occurs.

Group package:

Suppose a port to receive a fixed each other data source, only in this way, received a packet into the buffer, then ordered by the number of frames in the buffer (each frame of the serial number is the same, they can give each small pieces with small number, can add the data frame in baotou points total of how many piece, received a piece on the statistics, determine whether collect). When all is collected, the frame is called back to the upper layer with the packet header removed. When the frame data has not been collected in a certain period of time, it means that the transmission process has lost packets, and the received can be lost. When the upper layer should receive the callback data, it can be decoded and played. But before decoding, first check whether the sequence of frames is continuous. As video data,

If anything is missing, the sequence is dropped until the next i-frame. The serial number of each frame, preferably negotiated between the sender and the receiver, is brought with it when sending.