The 11 states of TCP
For logical clarity, assume that both active opening and closing connections are clients, and both passive opening and closing connections are servers
-
Client-specific :(1) SYN_SENT (2) FIN_WAIT1 (3) FIN_WAIT2 (4) CLOSING (5) TIME_WAIT
-
Server unique :(1) LISTEN (2) SYN_RCVD (3) CLOSE_WAIT (4) LAST_ACK.
-
(1) CLOSED (2) ESTABLISHED
1. LISTEN (server)
First, the server needs to open a socket to LISTEN.
When a service is provided, it is in the LISTENING state. A TCP state change is a change in the state of a port. When a service is provided, the default port is 80 and the default port is 21. After the FTP service is started, it is in the LISTENING state. When in LISTENING state, the port is open, waiting to connect, but not yet connected. It’s like the door to your house is open, but no one has come in yet.
After the client sends a connection request, it waits for a matching connection request: The client calls Connect through the application for active open. Client TCP then sends a SYN to request the establishment of a connection. /*The socket is actively attempting to establish a connection. Wait for a matching connection request */ after sending the connection request
2. SYN_SENT (client)
When requesting a connection, the client first sends a SYN_SENT signal to the machine to be accessed. At this time, the state is SYN_SENT and a timer is started. If no ACK is received after the timeout, the client resends the SYN. Normally, the SYN_SENT state is very short.
For example, if you want to access www.baidu.com, use TCPView to observe the connection ESTABLISHED by iexplore. EXE(IE), and you will find that the connection changes from SYN_SENT to ESTABLISHED soon, indicating that the connection is successful. The SYN_SENT state is fast and may not be seen.
3. Syn-received (server)
After receiving and sending a connection request, wait for the other party to confirm the connection request. When the server receives the synchronization signal from the client, it sends the ACK bit and SYN with 1 to the client. At this time, the server is in the SYN_RCVD state. If the connection is successful, it changes to ESTABLISHED.
4. ESTABLISHED
The ESTABLISHED state indicates that data is being transmitted between two machines. The most important thing to observe is which application is in the ESTABLISHED state.
5. FIN_WAIT_1 (client)
Active close The end application calls close, and its TCP sends a FIN request to close the connection and then enters the FIN_WAIT_1 state.
6. Fin-wait_2 (client)
After receiving the ACK from the server, fin-WaIT-2 is entered. This is the famous half-closed state, after the client and server shake hands twice to close the connection. In this state, the application is still able to receive data but can no longer send it, but it is also possible that the client will remain in FIN_WAIT_2 state and the server will remain in WAIT_CLOSE state until the application layer decides to turn this state off
7. CLOSE_WAIT (Server)
When server-side TCP receives the FIN, it issues an ACK in response to the FIN request (its receipt is also passed to the upper-layer application as end-of-file) and enters CLOSE_WAIT.
8. Time-wait (client)
After receiving the FIN from the server, the client sends an ACK packet and enters the time-wait state
The TIME_WAIT state, also known as 2MSL, is set to wait after TIME_WAIT2 has sent the last ACK packet. This state is set in case the last handshake packet is not sent. This is the safe status of the fourth handshake). This state largely ensures that both parties can finish on their own.
9. Last-ack (server)
The server has sent an ACK for the FIN to be sent by the client
After passively closing the end for some time, the application that receives the end-of-file will call CLOSE to CLOSE the connection. This causes its TCP to also send a FIN, waiting for an ACK from the other party. Last-ack is entered
10. CLOSING (Client)
CLOSING states occur in the case of “simultaneous CLOSING”. In this case, the “simultaneous closing” does not mean simultaneous in time, but refers to the case that the peer FIN is received before the FIN packet is confirmed.
11. Closed
After receiving the ACK packet, the server enters the closed state. Connect the end of the