Theory [8] Do you fully understand TCP&UDP?

1 Overview

Everyone should have heard about the TCP protocol and UDP protocol. Both TCP protocol and UDP protocol work at the transport layer, and their goals are to transmit data between applications. Our commonly used network communications, such as browsing the web, checking emails, telephone communications, etc., all transmit data through these two protocols.
Insert image description here

2 The difference between TCP and UDP

What is the core difference between TCP protocol and UDP protocol? Take a look at the picture below and you will know.
Insert image description here

TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol. Reliability is the biggest feature of TCP, which is mainly reflected in: no data loss, no errors, no out of order, and no duplicate arrivals. As shown above, TCP can transmit the "children" (data packets) to the recipient very securely.

UDP (User Datagram Protocol) is a connectionless, unreliable, and fast transmission transport layer communication protocol. Fast transmission is the biggest feature of UDP, which is mainly reflected in the fact that there is no need to establish a connection in advance before data is sent, and data can be transmitted more efficiently, but reliability cannot be guaranteed. As shown in the picture above, UDP is only responsible for sending the "child" (data packet), regardless of whether the recipient receives it or not.

After intuitively feeling the difference between the TCP protocol and the UDP protocol, let's take a look at how the TCP protocol works~

3 How does TCP work?

The connection management mechanism (reliable mechanism) of TCP communication is summarized as: three-way handshake to establish the connection and four-way wave to disconnect.

3.1 <Three-way handshake to establish connection>

TCP is a connection-oriented protocol, so every request sent requires confirmation from the other party. The TCP client and TCP server need to complete a three-way handshake before communicating to establish a connection.
Insert image description here
(1) The first handshake

Purpose: This is a data packet requesting to establish a connection. The client first sends a synchronization data packet to the server.

Contents of the TCP header of the data packet:
Synchronization SYN=1 (the client requests to establish a connection)
sequence number seq=x (the sequence number of the first byte of data transmitted by the client)

(2) Second handshake

Purpose: After the server receives the first data packet sent by the client, it determines that it is a data packet that actively establishes a connection. If the server agrees to the connection, the server responds by sending a data packet.

Contents of the TCP header of the data packet:
synchronization SYN=1 (the server establishes the connection)
confirmation mark ACK=1 (the server agrees to the connection)
sequence number seq=y (the sequence number of the first byte of data transmitted by the server)
confirmation number ack=x+ 1 (x byte data sent by the client has been received, and the client should be sent starting from the x+1th byte of data next time)

(3) The third handshake

Purpose: After receiving the confirmation from the server, the client sends a data packet to the server.

Contents of the TCP header of the data packet:

Synchronous SYN=1 (both parties have agreed to establish a connection)
confirmation mark ACK=1 (confirmation packet received from the server)
sequence number seq=x+1 (the sent packet is the x+1th byte of the data) confirmation
number ack =y+1 (receives y bytes of data sent by the server, and tells the server that it should start sending from the y+1th byte of the data next time)

Tips:
In the TCP data transmitted between the client and the server, the sequence number seq and confirmation number ack values ​​of both parties are calculated based on the seq and ack values ​​of each other. This ensures the continuity of TCP data transmission. Once the TCP data sent by one party is lost, the "handshake" cannot continue, thus ensuring the successful completion of the "three-way handshake".

3.2 <Wave four times to disconnect>

When the data transmission is completed, the TCP client and TCP server need to disconnect through four sessions. This process is called four waves.
Insert image description here
(1) Wave for the first time

Purpose: The client sends a connection release request packet to the server and stops sending data.

In the TCP header of the connection release packet:

Terminate FIN=1 (the client must actively release the TCP connection between the client and the server)

Serial number seq=x (x is specified by the client. Then wait for confirmation from the server)

(2) Wave for the second time

Purpose: After receiving the connection release message, the server sends a confirmation message to the client. Therefore, the connection in the client-to-server direction is released and the TCP connection is now in a semi-closed state. Half-closed because the client can no longer send data to the server, half-open because the server can still send data to the client and be received by it.

In the TCP header of the confirmation message sent by the server to the client:

Confirmation mark ACK=1 (received the data packet sent by the client and agreed to the client releasing the connection)

Confirmation number ack=x+1 (on the basis of receiving the client message, add 1 to its sequence number seq value as the value of the confirmation number ack of this segment of the message)

Serial number seq=y (y is specified by the server. Then wait for confirmation from the client)

(3) Wave for the third time

Purpose: If the server has stopped sending data to the client, release the connection and send a confirmation message to the client.

In the TCP header of the confirmation message:

Terminate FIN=1 (the server releases the TCP connection from the server to the client and no longer sends data to the client)

Confirmation mark ACK=1 (the data transmission from the server to the client is completed)

Confirmation number ack=x+1 (on the basis of receiving the client message, add 1 to its sequence number seq value as the value of the confirmation number ack of this segment of the message)

Serial number seq=z (z is specified by the server. Then wait for confirmation from the client)

(4) Wave for the fourth time

Purpose: After receiving the connection release message segment from the server, the client sends a confirmation message to the server.

In the TCP header of the confirmation message:

Terminate FIN = 1 (client and server disconnect)

Confirmation mark ACK=1 (received confirmation message from the server and agreed with the server to release the connection)

Confirmation number ack=z+1 (on the basis of receiving the server message, add 1 to its sequence number seq value as the value of the confirmation number ack of this segment of the message)

Sequence number seq=x+1 (on the basis of receiving the server message, use its confirmation number as the value of the sequence number of this segment of the message)

After understanding TCP, let’s take a look at how UDP works~

4 How does UDP work?

The UDP protocol is connectionless, that is, there is no need to establish a connection before sending data (no complicated three-way handshake and four-way wave process of the TCP protocol). Sending data simply encapsulates the data packet and then sends it out from the network card. There is no status relationship between packages. The UDP protocol is basically the interface between the IP protocol and the upper layer protocol. The UDP protocol uses the services provided by the IP layer to transfer the data obtained by the application layer from an application process on one host to an application process on another host.

For example, when the transport layer receives a UDP data message from the IP layer, it passes the UDP data message to the application process through the corresponding port according to the destination port in the UDP data message header, as shown in the figure below.
Insert image description here
As shown in the figure above, application process 4 and port 2, if the receiving UDP finds that the destination port number in the received message is incorrect (that is, there is no application process corresponding to the port number), the message is discarded and sent by ICMP. "Port unreachable" error message to the sender.

Tips ICMP (Internet Control Message
Protocol) is a sub-protocol of the TCP/IP protocol suite and is used to transmit control messages between IP hosts and routers. Control messages refer to messages about the network itself, such as network connectivity, whether the host is reachable, and whether routes are available.

5 To summarize

Data transmission seems simple, but it is actually very smart~

TCP and UDP serve programs, but programs are the same as people. You can communicate with each other, or you can talk at once. TCP means frank communication between each other. You can have timely feedback on whether the message has been received and whether the received information is consistent.
Insert image description here

Advantages: Reliable.
Disadvantages: slow transfer rate.
Applicable scenarios: Account login, payment and other related functions of communication software use reliable TCP.

UDP is all about talking. I don’t care whether you listen or not, whether you have feedback or not, I will keep talking.
Insert image description here

Advantages: Fast transmission rate.
Disadvantages: Unreliable.
Applicable scenarios: QQ, WeChat and other instant messaging software usually use UDP for point-to-point communication or audio and video calls with fast transmission.

In short, TCP and UDP each have their own advantages and disadvantages, it all depends on your actual needs~

Article reprint source

Guess you like

Origin blog.csdn.net/zhi_Alanwu/article/details/131243276
Recommended