This problem is seen accidentally on the net, feel examine Angle is more comprehensive.

Before answering this question, we need to understand TCP and UDP. Then, we focus on: 1. The differences between TCP and UDP 2. How to implement these differences with UDP conditions or basis.

The differences

I just got a reference map off the Internet.

In a nutshell, we can distinguish differences from the following aspects:

  1. Connection-oriented. TCP establishes a reliable connection before data transmission and transmits data as a byte stream. UDP does not need to be connected. It supports multicast and broadcast and is transmitted over the network in the form of data packets.
  2. Sequential data reception and retransmission. TCP ensures the order of data arrival and supports retransmission when packets are lost. UDP is not supported.
  3. Flow control and congestion control. TCP implements point-to-point communication on the basis of connections. The sender can adjust the transmission status of data flows according to the feedback information from the receiver. UDP just send.

UDP implementation of TCP

connection-oriented

The TCP connection is established from the “three handshake”, theoretically we can also simulate this way, using UDP to send three packets to simulate the connection establishment. Similarly, the “four-way handshake” of disconnection can be simulated through UDP packet sending.

Check and confirm packet loss problems

If the receiver receives the packet, it can acknowledge it by sending an ACK (as in TCP) to the sender. If some packets arrive early, the receiver can cache them. If some packets are lost, the receiver can set a timeout and ask the sender to resend the packets. The timeout duration is too short (compared with RTT), resulting in excessive retransmission. The timeout duration is too long, affecting the transmission speed.

Order problem

All packets have their own unique sequential ID, which may require modifying the protocol header. The receiver sends an ACK to tell the sender, “I am ready to receive a packet with a certain ID.”

Flow control

Referring to the sliding window protocol, the sender controls its own sending rate by the size of the receiver window embedded in ACK.

Will a deadlock occur?

When the sender receives a reply with a window of 0, the sender stops sending and waits for the receiver’s next reply. But if the reply with a non-zero window is lost during transmission, the sender keeps waiting while the receiver thinks the sender has received the reply and waits to receive new data, so the two parties wait for each other, resulting in a deadlock.

This timer is started every time the sender receives a reply with a zero window. When the time is up, it proactively sends a message to ask about the size of the recipient’s window. If the receiver still returns a zero window, reset the timer to continue waiting; If the window is not 0, it indicates that the reply packet is lost. In this case, the sending window is reset and the packet is sent to avoid deadlock.

Congestion control

Congestion control Applies to network exceptions, including packet loss and timeout.

Slow start algorithm: at the beginning, use small traffic to test the network quality, and exponentially increase the congestion window to avoid congestion. When the window size is equal to the threshold, the Windows are increased one by one instead. Congestion avoidance algorithm: When a timeout (congestion) occurs, we promptly adjust the window size to half of the current size, then the window size gradually increases, if the timeout (congestion) occurs again, then repeat the previous operation.

Additional functions: Fast retransmission algorithm: when the receiving end receives an out-of-order data packet, it immediately sends a double acknowledgement (do not wait for confirmation when your own data is sent). If the sender receives repeated acknowledgements for several times, it immediately retransmits the packets that the receiver has not received without waiting for the retransmission timer to expire. Fast recovery algorithm: When the sender receives multiple repeated acknowledgements, the network may not be congested (the sender may not even receive repeated acknowledgements because of congestion), but the congestion avoidance algorithm is adopted to prevent network congestion.