How does the TCP connection status change?

How does the TCP connection status change?

During the process of establishing and disconnecting the TCP client and server, there will be different state transition changes.

TCP is a connection-oriented reliable transmission protocol. TCP uses a three-way handshake and a four-way wave to establish and terminate connections. Through the three-way handshake, the sender and receiver exchange information such as sequence numbers and window sizes to ensure that both parties are ready for data transmission. During the transmission process, the connection is terminated normally by waving four times to ensure that the final data can be transmitted completely.

picturepicture

picturepicture


During the process of establishing and disconnecting the TCP client and server, there will be different state transition changes.

picturepicture


Status changes when establishing a connection:

1. Before establishing a connection, the status of both the server and the client is CLOSED.

2. After the server creates the socket, it starts listening and changes to the LISTEN state.

3. The client requests to establish a connection and sends a SYN message to the server. The client's status changes to SYN_SENT.

4. After receiving the client's message, the server sends ACK and SYN messages to the client. At this time, the server's status changes to SYN_RCVD.

5. After the client receives the ACK and SYN messages from the server, it sends ACK to the server, and the client status changes to ESTABLISHED.

6. After the server receives the ACK from the client, it also changes to ESTABLISHED.

At this point, the three-way handshake is completed and the connection is established!

picturepicture


Status changes when disconnecting (the server can also actively disconnect, taking the client's active disconnection as an example):

1. The client first sends a FIN message to the server, requesting to disconnect, and its status changes to FIN_WAIT1.

2. After receiving the FIN, the server sends an ACK to the client, and the server status changes to CLOSE_WAIT.

3. After receiving the ACK, the client enters the FIN_WAIT2 state. At this point the connection is half broken.

4. If the server still has data to send to the client, it will continue to send it. Until the transmission is completed, the FIN message is sent, and the server enters the LAST_ACK state.

5. After receiving the FIN from the server, the client immediately sends an ACK to the server. At this time, the client enters the TIME_WAIT state, and enters the CLOSED state after 2MSL (MSL refers to the maximum survival time of the message).

6. The server enters the CLOSED state after receiving the ACK from the client.

At this point, four waves are completed and the connection is over!

picturepicture


During the disconnection process, there are two points to note:

1. If the client sends a FIN message and receives the server's FIN before receiving the server's ACK, then the client replies ACK to the server and the status changes to the CLOSING state. After the client receives the server's ACK again, , the status changes to TIME_WAIT status.

2. If the client receives a message with both ACK and FIN flags from the server after sending a FIN message, it can directly enter the TIME_WAIT state without going through the FIN_WAIT_2 state.

picture