Computer network review---TCP/UDP difference

The difference between TCP and UDP

  • TCP is point-to-point, one-to-one; UDP can be one-to-one, one-to-many, many-to-one, and many-to-many
  • TCP is connection-oriented, UDP is connectionless. When TCP sends a request, it will first use the three-way handshake to connect, while UDP will not.
  • TCP is to provide reliable delivery, it can ensure that there is no error, no loss, no duplication, and orderly arrival. UDP does not provide reliable delivery.
  • TCP provides congestion control, but UDP does not
  • UDP supports broadcast, TCP does not support
  • TCP is oriented towards byte streams, UDP is oriented towards datagrams
  • Datagram size
    • **TCP: **Because TCP is a protocol built on the connection between both ends, there is no limit to the size of the data stream sent theoretically. However, due to the size limit of the buffer, if you send a large piece of data using TCP, it may be truncated into several pieces, and the receiver will receive it in turn.
    • **UDP: **Because UDP itself sends datagrams one by one, so naturally there is an upper limit size.

Then when do we have to use UDP:

  • **High real-time requirements: **For example, in real-time meetings and real-time video, if you use TCP, when the network is not good for retransmission, the picture will definitely be delayed, and even more and more piles. If UDP is used, even if a few packets are occasionally lost, it will not affect anything. In this case, it is better to use UDP;
  • **Multipoint communication: **TCP needs to maintain a long connection, so when it involves multipoint communication, it must establish its two-way connection with multiple communication nodes, and sometimes in a NAT environment, two communication nodes establish its direct connection. TCP connection is not an easy task, and UDP can be sent directly without maintaining the connection, so the cost will be very low, and the penetration is good. It is also correct to use UDP in this case.

1. Tcp guarantees reliability, Udp does not guarantee reliability

Tcp provides reliable services. The data transmitted through Tcp has no errors, no loss, no duplication, and an orderly reply. And Udp does its best to provide delivery, the processing logic is simple, the reliability is not guaranteed, the loss is not guaranteed, and the order is not guaranteed. Tcp will ensure reliability from these aspects:

1-1 connection-oriented

Tcp is connection-oriented. Three handshake is required to establish a connection before data is transmitted, and data can be sent only after the connection is successful. Udp does not need to establish a connection before sending data, it is connectionless. And the connection of Tcp is point-to-point, but Udp sending data supports one-to-one, one-to-many, many-to-one, and many-to-many.

1-2 Orderly confirmation

Each Tcp packet has a 32-bit sequence number. When establishing a connection, the starting sequence number will be agreed upon, and then sent one by one according to the sequence number, and in order to ensure that no packets are lost, an ACK response will be made to the sent packets. And the ACK is in order. For example, the packet with a large sequence number has arrived at the receiving end, but the packet with a small sequence number has not yet arrived. At this time, the receiving end cannot immediately respond to the packet with the large sequence number, but needs to wait for the packet with the small sequence number to arrive. And after replying, it is the turn of the packet with the larger serial number. Udp does not have this mechanism.

1-3 Retransmission mechanism

For each ACK that is sent but not received by the receiving end, Tcp will retransmit the packet when a packet has not received the corresponding ACK after a period of time. This is called a retransmission mechanism.

1-4 Congestion Control

Tcp will adjust the packet sending speed when packet loss and timeout retransmission occurs. The first is slow start. When there is congestion (packet loss, timeout retransmission), the sending speed will be reduced to adapt to the bad network environment. And Udp will not make corresponding changes in the network environment.

1-5 Maintained data structure

The cost of the above mechanism is the need to maintain additional data structures. For example, both parties need to maintain a state machine to establish a connection, and the order, retransmission mechanism, and congestion mechanism need to maintain a window to make corresponding records. Because of this, the header of Tcp is also more complicated than Udp. The more complicated it is, the more work is done, and the efficiency will naturally be lower.

2. Tcp is oriented to byte stream, Udp is based on datagram

The data transmitted by Tcp is transmitted in the form of byte stream, while Udp is transmitted in the form of datagram. When using Socket programming, if you are using Tcp, then both sending and receiving data get the inputStream or outputStream from the socket, and then read the data through the stream. If you are using Udp, the data will be encapsulated into a DatagramPacket when sending data, and the data will be received in DatagramPacket.

Guess you like

Origin blog.csdn.net/why1092576787/article/details/114791853