A brief introduction of TCP three-way handshake and four waving
TCP three-way handshake:
The first handshake: Client sets SYN to 1, randomly generates a value seq=J, and sends the packet to Server. Client enters SYN_SENT state and waits for Server confirmation.
Second handshake: After receiving the data packet, the Server knows that the Client requests to establish a connection from the flag bit SYN=1. The Server sets the flag bits SYN and ACK to 1, ack=J+1, randomly generates a value seq=K, and sends the data packet to the Client to confirm the connection request. The Server enters the SYN_RCVD state.
Third handshake: After receiving the acknowledgement, the Client checks whether the ack is J+1 and whether the ACK is 1. If it is correct, the flag bit ACK is set to 1, ack=K+1, and sends the data packet to the Server. The Server checks whether the ack is K+1 and whether the ACK is 1. If it is correct, the connection is successfully established. The Client and the Server enter the ESTABLISHED state to complete the three-way handshake. Then the Client and the Server can start transmitting data.
In short, it's
When a connection is established, the client sends a SYN packet (SYN=i) to the server and enters the SYN-SEND state, waiting for an acknowledgement from the server.
When the server receives the SYN packet, it must acknowledge the SYN (ack=i+1) of the client, and at the same time send a SYN packet (SYN=k), i.e., SYN+ACK packet. At this time, the server enters the SYN-RECV state.
The client receives the SYN+ACK packet from the server and sends an acknowledgement ACK (ack=k+1) to the server. After the packet is sent, the client and the server enter the ESTABLISHED state and complete the three-way handshake. The client and the server begin to transmit data.
TCP waved four times:
First wave: Client sends a FIN to close data transfer from Client to Server, Client enters FIN_WAIT_1 state.
The second wave: After receiving the FIN, the Server sends an ACK to the Client, confirming that the serial number is received +1 (the same as SYN, a FIN occupies a serial number), and the Server enters the CLOSE_WAIT state.
The third wave: Server sends a FIN to close the server-to-Client data transfer, and Server enters the LAST_ACK state.
The fourth wave: After the Client receives the FIN, the Client enters the TIME_WAIT state, and then sends an ACK to the Server, confirming that the serial number is received. The Server enters the CLOSED state and completes the four waves.