The difference between TCP and UDP (detailed explanation)

The difference between TCP and UDP (detailed explanation)


Before talking about the difference, understand the working mechanism and working principle of these two protocols

UDP:

UDP is a connectionless-oriented transport layer protocol, which cannot provide reliable transmission services.
Please add a picture description
It can be seen from the above message format that UDP messages can be divided into two parts: UDP header and UDP data. The header is composed of source port, destination port, packet length and checksum.
Source port (source port): the port number used by the source host application
Destination port (destination port): the port number used by the destination host
Length (packet length): the byte length of the UDP header and UDP data. Since the length of the UDP header is 8 bytes, the minimum value of this field is 8.
Checksum (checksum): Check the entire UDP segment, including the UDP header and UDP data. This value is calculated and recorded by the sender and verified by the receiver.

Based on the above UDP packet characteristics, in the actual application scenario, assuming that host A sends a data packet to host B, the larger data packet will be divided into several small data packets, and each data packet will be sent independently in the network. So different packets will reach host B through different paths. In this case, the data packet sent first may not be sent to host B first, because the UDP data packet has no sequence number, so host B cannot reassemble the data packets in the original order through the UDP protocol. At this time, the application program needs to provide Packet delivery determination, sorting and flow control functions.

UDP usually uses real-time transmission mechanism and time stamp to transmit voice and video data

Explanation: A timestamp is a complete and verifiable electronic certificate that can indicate that a piece of data has existed at a specific point in time. Time stamp (Digital Time Stamp) can provide accurate time proof for any electronic file, and can check whether the content of the file or transaction has been modified by anyone since it was time stamped.

Summary: UDP does not provide a retransmission mechanism, occupies less resources, and has high processing efficiency. Some delay-sensitive traffic, such as voice and video, usually uses UDP as the transport layer protocol.


TCP:

TCP is a connection-oriented transport layer protocol that provides reliable transport services

Please add a picture description
TCP is composed of two parts: TCP header and TCP data. The header is composed of some fields identified in the above figure. The analysis of the fields in the above figure is as follows:

Source port (source port): the port number used by the source host application
Destination port (destination port): the port number used by the destination host
Sequence Number (serial number): used to identify different TCP data segments sent from the sender serial number. The order of data segments will change during transmission, so the receiving end needs to reorganize the data according to the sequence number.
Acknowledge Number (confirmation sequence number): Used to identify the data segment that the receiving end confirms receipt of. The confirmation sequence number is the successfully received data sequence number + 1.
Header length (head length): identifies the number of 32-bit words in the header, and the maximum length of the TCP header that can be expressed is 60 bytes.
Window (window size): Indicates the size of the data that the receiver expects to receive through a single confirmation. This mechanism is usually used for flow control.
Checksum: Check the entire TCP field, including the TCP header and TCP data. This value is calculated and recorded by the sender and verified by the receiver.

TCP connection establishment process

Please add a picture description

From the above TCP establishment process diagram, we can see that the establishment of a TCP connection is a three-way handshake process. Host A sends a SYN data segment, indicating that it expects to establish a connection with server A. The sequence number of this data segment is a. Server A has restored the two data segments representing SYN+ACK. At this time, the sequence number of the data segment is b, and the sequence number is confirmed to be the sequence number of host A + 1, which is a+1. This message is used as a response to host A Acknowledgment of the SYN message. Then host A sends a data segment representing ACK, the serial number of the data segment is a+1, the confirmation serial number is the serial number of server A + 1, which is b+1, and this message is used as the SYN to server A Acknowledgment of the message.

In the TCP establishment process in the figure above, SYN is mainly used to synchronize data packets, and ACK is mainly used for a confirmation reply to received packets

TCP flow controlPlease add a picture description

TCP uses the TCP sliding window technology to control the flow, and realizes the data transmission control of the end-to-end device by dynamically changing the window size, so as to control the flow.
As shown in the figure above, the flow control is realized through the sliding window between host A and server A. In the case of the above figure, only when host A sends data to server A, server A controls the traffic through a sliding window.
Host A sends 4 data segments with a length of 1024 bytes to server A, where the window size of the host is 4096 bytes. After server A receives the third data segment, the buffer is full, and the fourth data segment is discarded. The server responds with ACK 3073, and the window size is adjusted to 3072, indicating that the server's buffer can only handle data segments of 3072 bytes. So host A changes its sending rate and sends data segments with a window size of 3072.

TCP closes the connection

Please add a picture description

TCP supports full-duplex mode (that is, it can receive data while sending data) to transmit data, which means that data can be transmitted in both directions at the same time. Before transmitting data, TCP actually establishes a connection in two directions through the three-way handshake, so after the transmission is completed, the connection in both directions must be closed.
The establishment of a TCP connection is a three-way handshake process, and the termination of a TCP connection requires a four-way handshake.
As shown in the figure above:
1. Host A wants to terminate the connection, so it sends a data segment that identifies FIN and ACK, the sequence number is a, and the confirmation sequence number is b.
2. Server A responds with a data segment marked with ACK, the sequence number is b, and the confirmation sequence number is a+1, as the confirmation of the FIN message of host A.
3. Server A wants to terminate the connection, so it sends a data segment that identifies FIN and ACK to host A, the sequence number is b, and the confirmation sequence number is a+1.
4. Host A responds with a data segment marked with ACK, the sequence number is a+1, and the confirmation sequence number is b+1, as the confirmation of server A's FIN message.
The above four interactions complete the closing of the connection in both directions.


The difference between TCP and UDP:

Through the above detailed explanation of the TCP and UDP protocols, let's look at the differences between TCP and UDP:

TCP UDP
connection-oriented (i.e. need to establish a connection) connectionless oriented
Oriented to byte streams (when sending data, the data will be decomposed into multiple small data packets for sending) Based on datagrams (when sending data, the UDP header will be directly added to send the entire message)
There are three handshakes to ensure the reliability of data transmission There may be packet loss in the transmitted data
Guaranteed data order Data order cannot be guaranteed
Only supports peer-to-peer communication Support one-to-one, one-to-many, many-to-many communication
With congestion mechanism no congestion mechanism
Header 20-60 bytes header 8 bytes
Requires low real-time performance and high accuracy Requires high real-time performance and low accuracy

Guess you like

Origin blog.csdn.net/qq_45714440/article/details/127879323