The difference and principle of TCP and UDP protocol

 
 

Recently, I re-recognized 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. The data generally includes: file type, video type, jpg image, etc.

Two, the difference

TCP is connection-based, while UDP is connectionless.

The tcp transmission data is stable and reliable , and is suitable for scenarios that require high network communication quality and needs to be transmitted to the other party accurately, such as transferring files, sending emails, browsing web pages, etc.

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

Take the communication between people as an example: the 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 cannot get timely feedback. The TCP protocol is like Call communication, you can get timely feedback in this series of processes, and can ensure that the other party receives it in time. As shown below:

3. The process of TCP communication:

How does tcp guarantee the above process?

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

Four, three-way handshake:

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

Why a three-way handshake instead of two?

This is to prevent, because the request message that has expired is suddenly transmitted to the server, causing an error.  What does this mean?

Assuming that a two-way handshake is used to establish a connection, the client sends a syn packet request to the server to establish a connection. Due to some unknown reasons, it does not reach the server, and a certain network node in the middle is stuck. 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 recovers, and the first syn packet is sent to the server. At this time, the server will think that the client has initiated a new connection, so it enters the waiting data state after the two handshakes, and the service The client considers it to be two connections, while the client regards it as one connection, resulting in an inconsistent state. If the server cannot receive the final ack packet during the three-way handshake, it will naturally not consider the connection to be established successfully.

So the three-way handshake is essentially to solve the problem of unreliable network channels. In order to establish a reliable connection on an unreliable channel, after the three-way handshake, both the client and the server enter the data transmission state.

5. Data transmission

data transmission:

A packet of data may be split into multiple packets for transmission. How to deal with the problem of packet loss. These packets arrive in different order. How to deal with the problem of out-of-order?

In response to these problems, the tcp protocol establishes a sending buffer for each connection. The serial number of the first byte after the connection is established is 0, and the serial number of each subsequent byte will increase by 1. When sending data, start from The data buffer takes a part of the data to form a sending message. The serial number and length will be attached to the tcp protocol header. After receiving the data, the receiving end needs to reply a confirmation message. The ack in the confirmation message is equal to the received serial number plus the length, and also It is the starting sequence number of the next packet data transmission. Such a question-and-answer sending method can make the sender confirm that the sent data has been received by the other party. The sender can also send continuous multi-packet data once. The receiver only needs to You only need to reply with an ack. 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 of hands are required to close the connection. Assuming that the client initiates a request to close the connection, it sends a FIN packet to the server, indicating that the connection is to be closed, and enters the termination waiting for 1 filling. After receiving the FIN packet, the server sends an ACK packet, indicating 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 still accept data, after the server sends the data, send a packet of FIN packet , and finally enter the confirmation state. This is the third wave . After receiving the ACK packet, the client restores the ACK packet and enters the timeout waiting state. After the timeout period, the connection is closed, and the server immediately closes the connection after receiving the ACK packet. This is the first time Wave four times .

Why is the client waiting for the timeout? This is to ensure that the other party has received the ACK packet, because it is assumed 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 you wait for a period of time, At this time, the server will resend the FIN packet because it has not received the ack packet, and the client will respond to the FIN packet to resend the ack packet and refresh the timeout period. 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-connected. To send data is to encapsulate a simple data packet and then send it out from the network card. There is no state connection between data packets. Because of the simple processing method of udp, As a result, its performance loss is very small. For CPU, the memory resource usage is much smaller than that of TCP. However, UDP cannot guarantee the 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, transferring files, sending emails, browsing web pages, etc. UDP has the advantage of fast speed, but may cause packet loss, so it is suitable for scenarios that require high real-time performance but do not require much packet loss. For example: domain name query, voice call, live video, etc.

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

Guess you like

Origin blog.csdn.net/gp_911014/article/details/127545134