The eleven states of TCP
This article is participating in the "Network protocol must know must know" essay contest
Because TCP is a duplex protocol, there is no difference between client and server. We will first define two concepts: the active requester usually refers to the client side, and the passive waiting side usually refers to the server side. Easy to understand the concept.
A total of state
- CLOSED Indicates that the TCP connection is not established or has been completely released. In real scenarios, the two parties are disconnected from each other. There are two types of state transitions:
- Active Open Active access requests (client requests).
- Passive Open Waiting for the new connection status of the client (the server exposes the port to wait for access)
- ESTABLISHED State After receiving the SYN+ACK value from the server, the client returns an ACK value. If the ACK value is received successfully, both parties enter the ESTABLISHED state, indicating that the connection is ESTABLISHED.
Client exclusive status
- Syn-sent Status After a client sends a SYN packet, the client waits for an ACK from the server. In this state, a SYN is sent and a timer is started synchronously. If an ACK is not received after the timeout, the SYN is resended.
- Close-wait State After receiving the FIN packet in fin-WaIT-1 state from the client, the server enters the close-wait state after sending the ACK packet.
- Fin-wait-1 The client attempts to send a FIN packet and enters the FIN-WaIT-1 state when the server replies with ACK. (The server can also send a FIN packet to end the session. This is just for the convenience of description and understanding.)
- Fin-wait-2 A connection in the FIN-WaIT-1 state enters the FIN-WaIT-2 state after receiving an ACK packet. In this case, the FIN packet is confirmed by the client and the client waits for the server to send the FIN packet. After receiving the FIN packet, the connection enters the time-wait2 state.
- Time-wait After receiving the FIN packet from the passive closing party, the client sends an ACK confirmation to enable the 2MSL timer (generally 120S). When the timer expires, the client enters the CLOSED state and releases the connection.
Server exclusive status
- LISTEN When the bind and LISTEN system calls are invoked to LISTEN for a specific port, the server enters the LISTEN state and waits for the client to send a SYN packet to establish a connection.
- Syn-rcvd Status After receiving a SYN packet, the server replies with SYN+ACK and enters the SYN-RCVD state while waiting for ACK from the client.
- Last-ack Status Indicates the status of the FIN packet sent to the server for confirmation after receiving the final ACK packet from the client.
In full-duplex mode, when the initiator sends the FIN confirmation packet but does not receive the confirmation packet, the packet is disabled. When the other party sends a FIN request to close the connection, it will enter the CLOSING state. After entering the CLOSING state, it will send an ACK packet to the other party to complete the CLOSED state and finally close the connection.
Specific case analysis
Just look at the above state introduction is not confused, let's introduce a specific case (TCP three-way handshake and four-way wave) to explain the conversion process of each state in the actual transmission process.
The initial unconnected state is called the CLOSED state, which means that both the server and the client are waiting to interact. The first handshake confirmation is performed when the client sends the request
- Client update: syn-sent status
- Server update: LISTEN Status When the server sends the SYN_ACK packet to the client, the server waits for the reply and performs the second handshake
- Server update: SYN-rcvd Status After the client replies with an ACK, the client enters the ready state and changes the status after the server receives an ACK
- After sending ACK, the client changes to ESTABLISHED and waits for data to be sent
- After receiving the ACK, the server also changes to ESTABLISHED and enters the communication state
As the TCP protocol is a duplex protocol, both parties can send ACK to each other at the same time to enter the first handshake. When both parties send information to each other at the same time, perform the first handshake confirmation (temporarily a, B for the communication parties)
- A The SYN packet enters the SYN-sent state
- User B also enters the SYN-sent state after sending a SYN packet. If user A receives a SYN packet from USER B while waiting for the ACK+ ACK reply from user B, user A passively changes to SYN-RCVD and replies to USER B with a SYN+ACK packet
- After receiving a SYN packet from USER B, user A replies with a SYN+ACK packet from user B and changes the status to SYN-RCVD
- After receiving the SYN+ACK packet from A, USER B sends the ACK packet to User A for the second handshake
Four times to wave
The initial state of both the client and server is ESTABLISHED because the four-wave occurs when the connection is disconnected. After the client sends the FIN packet, the request is disconnected and the first wave is performed. (After the client sends the FIN packet, it cannot send data to the server, but can continue to receive data from the server. This state is also called the half-closed state.)
- After the client sends a FIN packet, the status is changed to fin-wait1. After receiving a FIN packet, the server replies with an ACK packet. If there is incomplete data, the server can continue sending the PACKET
- The server status is changed to CLOSE_WAIT
- After receiving an ACK packet, the client changes its STATUS to FIN-Wait2. The server continues to send the FIN packet to the client
- The server status was changed to last-ACK. Procedure
- The client will send the last ACK packet and WAIT for two MSLS
- The client status is updated to CLOSED
- After receiving the ACK, the server closes the link and changes the status to CLOSED
This is the end of the TCP state, personal understanding, if there is any error or deviation, also hope you can point out the correction, thank you for taking the time to read here.