Network (11): The difference and principles of TCP and UDP protocols

The difference and principles of TCP and UDP protocols

Recently, I have re-understood the principles and differences between TCP and UDP, and made a simple summary.

1. Function

First of all, both tcp and udp work at the transport layer and are used to transmit data between programs. Data generally includes: file type, video type, jpg pictures, etc.

2. Difference

TCP is connection-based, while UDP is connection-less.

TCP transmission data is stable and reliable , and is suitable for scenarios that require high network communication quality and require accurate transmission to the other party, such as transferring files, sending emails, browsing the web, etc.

The advantage of UDP is that it is fast , but packet loss may occur, so it is suitable for scenarios that require high real-time performance but do not have high requirements for a small amount of packet loss. For example: domain name query, voice call, video live broadcast, etc. Another very important application scenario of udp is tunnel network, such as: VXLAN

Take communication between people as an example: UDP protocol is equivalent to writing a letter to the other party. After sending the letter, you cannot know whether the other party has received the letter, whether the content of the letter is complete, and you cannot get timely feedback, while the TCP protocol is like By calling and communicating, you can get timely feedback in this series of processes and ensure that the other party receives it in time. As shown below:

3. TCP communication process:

How does tcp ensure the above process?

It is divided into three steps: three-way handshake, transmission confirmation, and four waves . The three-way handshake is the process of establishing a connection.

4. Three handshakes:

When the client initiates a connection to the server, it will first send a packet of connection request data. Go over and ask if I can establish a connection with you? This packet of data is called a SYN packet. If the peer agrees to the connection, it will reply with a SYN+ACK packet. After the client receives it, it sends an ACK packet and the connection is established, because three packets of data are sent to each other in this process. So it's called a three-way handshake.

Why a three-way handshake instead of a two-way handshake?

This is to prevent errors caused by invalid request messages suddenly being transmitted to the server.  What does this mean?

Assume that a two-way handshake is used to establish a connection. The client sends a syn packet to the server to request to establish a connection. For some unknown reasons, it does not reach the server and is stuck at a network node in the middle. In order to establish a connection, the client will Resend the syn packet, this time the data packet is delivered normally, and the connection is established after the server sends syn+ack.

However, the network blocked by the first packet of data suddenly recovered, and the first syn packet was delivered to the server. At this time, the server will think that the client has initiated a new connection, and will enter the waiting data state after the two handshakes. The client thinks there are two connections, but the client thinks it is one connection, resulting in inconsistent status. If the server does not receive the final ack packet during the three-way handshake, it will naturally not think that the connection is established successfully.

Therefore, the three-way handshake is essentially to solve the problem of unreliable network channels and to establish a reliable connection on unreliable channels. After the three-way handshake, both the client and the server enter the data transmission state.

5. Data transmission

data transmission:

One packet of data may be split into multiple packets and sent. How to deal with the packet loss problem? These data packets arrive in different orders. How to deal with the out-of-order problem?

In response to these problems, the TCP protocol establishes a sending buffer for each connection. The sequence number of the first byte after the connection is established is 0, and the sequence number of each subsequent byte will increase by 1. When sending data, from The data buffer takes part of the data to form a sending message. The sequence number and length will be attached to the TCP protocol header. The receiving end needs to reply with a confirmation message after receiving the data. The ack in the confirmation message is equal to the received sequence number plus the length, which is also It is the starting sequence number for sending the next packet of data. Such a question-and-answer sending method can enable the sending end to confirm that the sent data has been received by the other party. The sending end can also send consecutive multiple packets of data at once. The receiving end only Just need to reply with ack once. As shown in the picture:

Six or four waves:

Both the client and the server in the connected state can initiate a request to close the connection. At this time, four waves are required to close the connection. Assume that the client actively initiates a connection closing request. It sends a FIN packet to the server to indicate that the connection is to be closed. It enters the termination wait 1 to fill. The server receives the FIN packet and sends an ACK packet to indicate that it has entered the shutdown wait. state, the client enters the termination waiting 2 state. This is the second wave . The server can still send unsent data at this time, and the client can also receive data. After the server finishes sending the data, it sends a FIN packet. , and finally enters the confirmation state. This is the third wave . After receiving the ACK packet, the client restores the ACK packet, enters the timeout waiting state, and closes the connection after the timeout period. After receiving the ACK packet, the server immediately closes the connection. This is the third time . Four waves .

Why does the client have to wait for the timeout? This is to ensure that the other party has received the ACK packet, because assuming that the client releases the connection after sending the last ACK packet, once the ACK packet is lost in the network, the server will stay in the final confirmation state. If it waits for a period of time, At this time, the server will resend the FIN packet because it has not received the ACK packet. The client will respond to the FIN packet, resend the ACK packet, and refresh the timeout. This mechanism is the same as the third handshake. It is also to ensure reliable connection disconnection confirmation in unreliable network links.

7. UDP protocol

udp: First of all, the udp protocol is non-connection. To send data, you just need to encapsulate a simple data packet and then send it out from the network card. There is no status connection between the data packets. Because of the simple processing method of udp, As a result, its performance loss is very small. The CPU and memory resource usage are much smaller than tcp. However, udp cannot guarantee packet loss during network transmission, so udp is weaker than tcp in terms of transmission stability.

Therefore, the main difference between tcp and udp: Tcp transmission data is stable and reliable, suitable for scenarios that require high network communication quality and need to be transmitted to the other party accurately. For example, when transferring files, sending emails, browsing the web, etc., the advantage of UDP is that it is fast, but packet loss may occur, so it is suitable for scenarios that require high real-time performance but do not have high requirements for a small amount of packet loss. For example: domain name query, voice call, video live broadcast, etc.

Another very important application scenario of udp is tunnel network, such as VXLAN.

Reposted from: Must-learn for testing: illustrated with pictures and texts, explaining the principles and differences between TCP and UDP protocols

Guess you like

Origin blog.csdn.net/qq_37674086/article/details/126365491