Computer Network (11)-Have you heard of TCP three-way handshake?

In the previous article, we introduced the simple structure and functional characteristics of UDP. Now the shotgun is replaced by the cannon, let's take a look at the highlight of the transport layer-the TCP protocol.

The TCP protocol needs to establish a session before data transmission for full-duplex communication, and has the functions of reliable transmission, flow control, and congestion avoidance.

In this section, we will introduce the header format of the TCP message and its three-way handshake and four waved hands (connection management).

1. TCP header format

1. Source port/destination port: the same as in UDP, each occupying 2 bytes

2. Sequence number and confirmation number: each occupies 4 bytes, which is related to the byte stream-oriented characteristics of TCP. TCP stores the data to be sent in the TCP buffer and sends them in sequence. Sequence number Even though the first byte number sent in this message, the confirmation number is the first byte number expected to receive the byte stream.

3. Data offset: the offset of the data part of the TCP message segment from the start of the TCP message segment, that is, the length of the header, the unit is 4B and occupies 6 bytes

4. Reserved bit: occupies 6 bytes

5. Six flag bits, each occupying 1 byte:

​ 1) URG: Emergency flag, the segment with UGG=1 has priority to send

​ 2) ACK: Acknowledgement flag bit. In the segment of ACK=1, the acknowledgment number is meaningful. After the connection is established, ACK=1 for all messages

​ 3) *PSH: Push bit

​ 4) *RST: Reset, when a serious error occurs in the TCP connection, release the connection and re-establish a new connection

​ 5) SYN: synchronization bit, the segment with SYN=1 is a connection request\connection acceptance message

​ 6) FIN: Abort bit, FIN=1 indicates that the message segment has finished sending data, requesting to release the connection

6. Window: Tell the other party the size of the receiving window for easy flow control

7. Checksum: The check algorithm is the same as UDP, except that the protocol field in the pseudo header is 6, instead of 17.

8. Urgent pointer: When URG=1, it indicates the number of bytes of urgent data in this segment

9. Options: Maximum message length MSS, window expansion, time stamp, selection confirmation, etc.

10. Padding: Due to the variable size of the option part, the filling part will fill the TCP header with an integer multiple of 4 bytes

Two, TCP connection management

​ What is connection management? TCP is a reliable transmission based on full-duplex, all of which are based on TCP's need to establish a connection before data transmission. Connection management is mainly divided into two parts, namely, the three-way handshake for establishing a connection and the four waves for releasing the connection.

1. Three-way handshake

The first handshake :

​ The client sends a SYN segment to the server, SYN=1, seq=x, and the client enters the SYN-SENT state

Second handshake :

​ After receiving the SYN segment, the server allocates buffers and variables for the TCP connection, and returns a SYN segment, SYN=1, ACK=1, seq=y, ack=x+1, and the server enters the SYN-RECD state

The third handshake :

​ After the client receives the SYN message from the server, it allocates buffers and variables for the TCP connection, and returns an ACK message, ACK=1, seq=x+1, ack=y+1, which can carry data, the client The connection is successfully established and enter the ESTABLISHED state.

​ After the server receives the ACK message, the connection is successfully established and enters the ESTABLISED state.

2. Wave four times

First wave :

​ The client sends a FIN message, requesting to close the connection, FIN=1, seq=u, and the client enters the FIN-WAIT-1 state

The second wave :

​ The server receives the FIN message and returns an ACK message, ACK=1, seq=v, ack=u+1, and the client enters the CLOSE-WAIT state.

​ At this time, the TCP connection is half-closed-the client does not transmit data to the server, but the server can still transmit data to the client

Third wave :

​ The server sends a FIN message to the client, FIN=1, ACK=1, seq=w, ack=u+1, and the client enters the LAST-ACK state

Fourth wave :

​ The client sends an ACK message to the server segment, ACK=1, seq=u+1, ack=w+1, after waiting for 2MSL, it enters the CLOSED state

​ After receiving the ACK message, the server enters the CLOSED state

3. Connection management answering questions

In a programmer interview, three handshake and four wave of hands are common topics of interviewers. Here are some common questions:

Q: Can the three-way handshake be changed to two-way handshake?

Answer: No, it is prone to deadlock. If the connection is established using only two handshake, the server will consider that the connection has been established successfully after receiving the SYN message, and return a confirmation message and application data at the same time. If the confirmation message is lost at this time, the client considers that the connection establishment has failed, and will ignore all messages sent by the server and wait for the confirmation message. The stupid server sends a message to the client, but fails to get the confirmation message from the client, and also enters the waiting state. The two wait for each other, forming a deadlock.

Question: Why does the client need to send an acknowledgement message at the end of the three-way handshake?

Answer: In order to avoid the invalid connection request being sent to the server suddenly, causing errors.

​ In reality, there may be such a situation: the first SYN message sent by the client to the server, due to network reasons, the transmission is slow and does not arrive immediately. So the client sent the second SYN message, and it quickly reached the server, successfully established the connection, completed the data transmission, and released the connection. Then the first SYN that got lost suddenly reached the server successfully. The server mistakenly thought that the client had initiated the request again, and then established a connection and transmitted data again, thus wasting resources.
​ And if there is a third confirmation, even if the first SYN message that has expired causes the server to return a SYN message, the client will not confirm this message, and the two parties will not establish a second connection.

Q: Why is there three handshake and four wave hands?
Answer: The second handshake is equivalent to combining the second wave and the third wave into one, so one is omitted

Q: Why wait for 2MSL after the fourth wave?

Answer The ACK message for the fourth wave may be lost. The server that cannot receive the ACK message will resend the SYN message for the third wave. If the client during the 2MSL waiting period receives the retransmitted SYN message, The ACK message for the fourth wave of hands will be retransmitted. If it is not received, it is considered that the fourth wave of hands has arrived successfully and the connection is closed. MSL is usually set to 75s

Q: What should I do if the client suddenly fails after the connection is established?
Answer: TCP has a keep-alive timer. If the client fails, the server will not wait forever. The server resets the timer every time it receives a request from the client, and the time is set to 2 hours. If it does not receive the client's data within two hours, the server sends a detection message, which is sent every 75s. If no response is received for 10 consecutive detection messages, the server determines that the client is faulty and directly closes the connection

Guess you like

Origin blog.csdn.net/weixin_44580146/article/details/106965891