TCP three-way handshake

The flow chart of the total

Client->Server: SYN=1
Server->Client: SYN=1,ACK=1
Client->Server: ACK=1
Copy the code

First handshake: Client -> Server

Client->Server: SYN=1,seq = x
Copy the code
  • In this handshake, the client sends a request to establish a connection to the server. In this case, the client cannot determine whether the server receives the request packet. Therefore, the TCP connection is not established successfully.
  • Before this step, the client is in the CLOSED state, and when the Server is ready to receive a client connection request, the Server changes to the LISTEN state
  • After the Client sends a SYN packet with the value of 1, it enters the SYN-send state
  • The first handshake consumes a sequence number, SEQ, on the client

Second handshake: Server->Client

Client->Server: SYN=1; seq=x Server->Client: SYN=1,ACK=1; ack=x+1,seq=yCopy the code
  • In the process of the second handshake, the server connection request message received from the client, at this time can guarantee the normal data can be sent to the server for the client, but the client still don’t know whether the server is received to establish a connection request packet, so the service side should continue to be a response to the client, to inform the client, agreed to receive a TCP connection is established
  • After the server agrees to the connection request from the client, the syn-RCVD state is entered
  • The server also consumes its own serial number when sending to the client

Third handshake: Client->Server

Client->Server: SYN=1; seq=x Server->Client: SYN=1,ACK=1; ack=x+1,seq=y Client->Server: ACK=1; ack=y+1; seq=x+1Copy the code
  • After receiving the packet from the server, the client sends an acknowledgement packet to inform the server that the client has received the packet agreeing to establish a connection. After the packet is sent, the client enters the ESTABLISHED state, indicating that the connection is ESTABLISHED
  • After receiving the ACK packet sent by the client, the server enters the ESTABLISHED state. The connection is ESTABLISHED and communication can start

Why the third handshake?

  • To prevent invalid connection request packets from being sent to the server, an error may occur. If the client connection request message sent in the first place because the network reason stranded, does not arrive even if the service side, so for a long time have not received the server returns the response message, the client will be resend, and retransmission packet and been received by the server and through the second handshake connection is established, at the same time closed the connection after completed the information transmission, Results were stranded in the network connection is established for the first time the request was finally arrived at the server, the server will think the client to establish a connection, so can response to the client, because there are only two shake hands at the same time, the two sides and into a state of connection is established, but there is no any data communication between both sides, and waste of resources. If there is a three-way handshake and the server does not receive an acknowledgement packet from the client, the server knows that the client has not established a connection with it and releases the connection.

Four times to wave

The flow chart of the total

Note left of Client: close the TCP connection Client -> Server: FIN=1; seq=u Server -> Client: ACK=1; Ack =u+1,seq= V Server --> Client: data transmission Note Right of Server: Server -> Client: FIN=1; seq=w,ack=u+1 Client -> Server: ACK=1; seq=u+1; ack=w+1Copy the code

First wave

Note left of Client: close the TCP connection Client -> Server: FIN=1; seq=uCopy the code
  • Before the four waves, both sides were communicating normally, both in ESTABLISHED. When the communication ends, the client disconnects. In this case, the client sends the first wave packet to the server and enters the FIN-WaIT-1 state

Second wave

Note left of Client: close the TCP connection Client -> Server: FIN=1; seq=u Server -> Client: ACK=1; ack=u+1,seq=vCopy the code
  • After receiving the disconnection packet from the client, the server knows that the client is about to close the current TCP connection. Therefore, it responds to the disconnection by telling the client that the server knows that the client is about to close the connection.
  • After the server responds to the CLIENT’s FIN packet, it enters the close-wait state instead of disconnecting immediately. In this case, the communication between the Client and the Server is interrupted. The Client cannot send data to the Server, but the Server can continue to send data to the Client
  • After receiving the confirmation packet from the server, the client enters the FIN-WaIT-2 state

Third wave

Note left of Client: close the TCP connection Client -> Server: FIN=1; seq=u Server -> Client: ACK=1; Ack =u+1,seq= V Server --> Client: data transmission Note Right of Server: Server -> Client: FIN=1; seq=w,ack=u+1Copy the code
  • After the second wave, the server continues to send some unfinished data to the client. When all data is transferred and the TCP connection is to be closed, the server should also disconnect itself from the client. Therefore, the device proactively sends the third wave packet to the client.
  • After the third wave packet is sent, the device enters the last-ACK state.

Fourth wave

Note left of Client: close the TCP connection Client -> Server: FIN=1; seq=u Server -> Client: ACK=1; Ack =u+1,seq= V Server --> Client: data transmission Note Right of Server: Server -> Client: FIN=1; seq=w,ack=u+1 Client -> Server: ACK=1; seq=u+1; ack=w+1Copy the code
  • After receiving the disconnection packet from the server, the client also closes the data channel sent from the server to the client. In this case, the client sends an ACK packet to the server, and then enters the time-wait state. After 2MSL, the client automatically enters the CLOSED state and the TCP connection is CLOSED.

Why wait 2MSL before entering the CLOSED state

  • Ensure that the final ACK packet received by the service end, because if the server is not received the client returns an ACK, so the server disconnected will think you send the request message (wave) for the third time lost, then the server will be resend, and if I didn’t get a service within the client again 2 MSL to resend the package, This means that the server has received the ACK for the fourth wave, and the connection is normally disconnected
  • In 2MSL time, it can make all the packets in this connection disappear without affecting other connections. If you do not wait, in case the client disconnects directly and immediately creates a NEW TCP connection, and the connection information such as port is consistent, then because there is no wait 2MSL, the network stranded packets will be sent to the server during the connection, and the data sent is garbage data for the connection