A brief description of the TCP three-way handshake, and the difference between TCP and UDP

1. Brief description of TCP

1.1, TCP three-way handshake to establish a connection

First, let's take a look at the TCP header, as shown in the figure below.
Insert picture description here
Among them, we must understand the serial number [seq] and confirmation number [ack] in the above figure. Because TCP is a reliable connection, TCP will add a number to each byte in the byte stream. If the sequence number is 0, it means that the first byte to be sent is numbered 0. If the data length is 10, then The byte number of the message is 0-9. The acknowledgment number indicates that the receiving end correctly received the byte with the sequence number N, and the sending end is required to send the next byte with the sequence number N+1. For example, if the current ack is 10, it means the first ten Bytes, that is, the bytes numbered 0-9 have been received, the next byte to be received is the eleventh byte, that is, the byte number is 10.
Also need to look at two flags, one is the confirmation flag ACK, when the ACK is 1, the confirmation number is valid. There is also a SYN field. When the SYN field is 1, it means that this is a connection request or connection acceptance message.
Next, let's look at the detailed process of the TCP three-way handshake.
Insert picture description here
It can be seen from the figure above that in the first handshake, the client sets the SYN flag to 1, indicating that this is a connection request, and randomly generates a sequence number x, and sends the data packet containing the SYN flag bit and sequence number x to the service After that, the client will enter a sent state. In the second handshake, when the server receives the data packet from the client, it can be known from the 1 of the SYN flag that the client wants to establish a connection, and the server sets both the SYN flag and the ACK flag to 1, and sets the ack The value is set to x+1 and a sequence number y is randomly generated. The ACK flag bit is 1 to indicate that ack=x+1 is valid. The value of ack is x+1, which indicates that the server successfully received the byte with sequence number x. In the third handshake, the client confirms again, setting the serial number of the data to be sent to x+1, ACK to 1, ack to y+1, and serial number to x+1 because of the second handshake The server sets the ack of the sent data to x+1, indicating that the next time the server accepts the data from the client, the sequence number should be x+1, ACK is set to 1 and ack is set to y+1, and the client has successfully received the data from The second handshake of the server. At this point, the client and server have successfully established a connection. The three-way handshake in the figure above can be simplified to the steps shown in the figure below.
Insert picture description here
Why does TCP need to perform a three-way handshake instead of two?
In the first handshake, the server knows that it is normal in receiving data and the client in sending data. In the second handshake, the client knows that its sending and receiving and the server's sending and receiving data are normal. In the third handshake, the server knows that its sending and client receiving are normal. Therefore, after the three-way handshake, both the client and the server can confirm that their data transmission and reception are normal, and then the connection can be established.

1.2, TCP waved four times to disconnect

First, let's take a look at the outline of the four waved hands, as shown in the figure below.
Insert picture description here
As can be seen from the above figure, when the client wants to close the connection, it will send a message with the FIN flag as 1 to the server, telling the server that the client is about to close the connection, and then the client enters the WAIT1 Waiting state, the server will send a message with ACK set to 1 to the client after receiving the message from the client, indicating that the server knows that the client is about to close the connection, but the server may still need to process it at this time Data, so the server will first process the data to be processed, and then send a message to the client with FIN set to 1 to tell the client that the server has processed the data to be processed, and the connection can be closed. The client will send a message with the ACK set to 1 to the server to confirm the information, and then the server can close the connection. The client will also shut down after waiting for a time of 2 times the MSL [Maximum Lifetime of Message].
Why does the TCP handshake need to be done three times and the wave hand need to be done four times?
In fact, in essence, the TCP handshake is actually four times, but the two in the middle can be merged, and the middle two steps of the wave cannot be merged, because there is a server data processing for sending an ACK message and sending a FIN message. The time interval does not happen at the same time.

2. The difference between TCP and UDP

The header of TCP is a bit more complicated than that of UDP. The former has a fixed length of 20 bytes, while UDP has only 8 bytes, so the transmission efficiency is relatively high. The headers of the two are shown in the figure below.
Insert picture description here
Insert picture description here
UDP is an unreliable communication protocol. It does not need to establish a connection, and does not require ack confirmation. It is not responsible for any disorder or loss of transmitted data. Therefore, UDP is suitable for real-time video calls and voice chats. TCP is a connection-oriented and reliable data transmission service. It requires a three-way handshake to establish a connection before data transmission can be carried out. TCP has a timeout retransmission mechanism and sliding window flow control technology to ensure that data will not be lost or lost during transmission. Out of order, reliable transmission is ensured. Therefore, TCP is suitable for data integrity and high communication quality, and is more suitable for file downloading and web browsing.
TCP is byte stream oriented, which means that the data is delivered to the receiver in the form of a byte stream. Each TCP will have a sending buffer. If the byte stream is too long, it exceeds the sending buffer. Capacity, then TCP will split the byte stream, and then send a byte stream of the sending buffer capacity, if the byte stream is too short, TCP will wait for the byte stream in the buffer to arrive at the right time before sending it out [for example , When the sending buffer receives more byte streams], the data received by the receiver may be several packets, or one or half. The UDP transmission message is controlled by the application layer. No matter how long the message is, UDP will be sent out. It will not be split or merged. The boundary of the message is preserved. Therefore, the following summary diagram of the difference between the two can be obtained. .
Insert picture description here

Guess you like

Origin blog.csdn.net/ISs_Cream/article/details/109908287