Why are TCP interactions reliable and udp interactions unreliable? Can UDP be reliably transmitted? With that question in hand, I looked up some information, and I wrote down the answer to that question.


What is a TCP connection

  • TCP’s three handshakes and three waves were described earlier. As FAR as I understand, THE TCP connection is to determine the accessibility of the interaction between the two parties, but confirming the unique interaction channel between the two parties does not guarantee the correctness of the data. Various errors still occur in the transmission of messages

The connection itself does not guarantee data reliability, so how

Data errors that can occur

  • Due to the complexity of the network during the transmission of out-of-order data, the byte stream sent continuously may not arrive in order
  • Data exception Affected by the environment, the electrical signal may jump and make the data abnormal
  • Packet loss
  • Duplicate message

ACK and timeout retransmission mechanisms

  • Data is out of order. The last correct data received by the receiver is SEq + 4, which returns SEq + 5 as an ACK. It receives SEq + 7 in the wrong order, so the receiver will return SEq + 5 to the sender again
  • Data error. Every TCP data carries a checksum of the data. After receiving the data SEQ + 3, the receiver will verify and verify the data first. If the result is incorrect, ACK seq + 3 is sent and the sender resends the data
  • Data loss or delay. The sender starts a timer when sending seQ data. If it does not receive ACK SEQ + 1 within the specified time, it sends the seQ data again
  • Data duplication. The receiver simply discards the duplicate data

Optimization of ACK mechanism

  • The sliding window
  • Delay ACK
  • Congestion control

Use UDP for reliable transmission

  • Override TCP logic at the application layer

UDP to be reliable, will be the receiver reply a confirmation after receive the UDP packet, the sender has a mechanism, don’t get confirmation will resend package, each package has a serial number increasing, the receiver found among lost package will be send the retransmission request, when the poor network packet loss frequently, prevent the packet loss the vicious cycle of retransmission, to send window there is a limit, The size of the sending window should be adjusted according to the network transmission condition, and the adjustment algorithm should have certain adaptability