The difference between TCP and UDP (usage scenarios)

TCP:

1) Connection-oriented (three-way handshake to establish connection) (reliable transmission)

2) Provide reliable services

3) Byte stream-oriented: Byte stream-oriented is to send data in bytes, and a data packet can be split into multiple data packets based on byte size to facilitate sending.

4) Only supports point-to-point communication: both parties need to establish a connection.

5) The maximum size of the TCP header is 20 bytes

UDP:

1) Connectionless (no need to establish a connection when sending)

2) We will try our best to deliver, but reliable delivery is not guaranteed (it is not reliable, so we cannot guarantee that the other party will receive it)

3) For messages,

4) UDP supports one-to-one, one-to-many, and many-to-many

5) UDP is only 8 bytes.

How TCP ensures its reliability:

1) The data packet check is calculated by the sending end, and then verified by the receiving end (16 is the data checksum) to prevent the data sent from being wrong data. 2) Confirm the sequence number, which is used to track the amount of data sent by the end and correct for out-of-
sequence Messages are rearranged
3) TCP fragments transmitted as IP datagrams will be repeated, and the TCP receiving end must discard duplicate data
4) Confirmation response mechanism, each time the receiver receives the data, it will perform a check on the transmitter Confirmation response, that is, an ACK message will be sent.
5) Timeout retransmission mechanism. After sending a certain data, a timer is started. If the ACK message of the sent datagram is not received within a certain period of time, the data will be resent. Until the sending is successful.
6) Flow control (16-bit window size) The so-called flow control is to prevent the sending rate from being too fast, so that the receiver can receive in time. Ensure that the data received by the receiver will not overflow in its own buffer
7) Congestion control to ensure the reliability of data propagation in the network, reduce the probability of packet loss, and improve the reliability of TCP

The reason why UDP is unreliable:
There is no above-mentioned TCP mechanism and if the checksum is wrong, UDP will discard the message

When to choose TCP or UDP

For situations where real-time requirements are relatively high, choose UDP, such as IP telephony, video conferencing, games, media communications, and live broadcasts.

For reliable transmission applications, choose TCP, such as transferring files.

Guess you like

Origin blog.csdn.net/Anyo1n/article/details/126837270