TCP three-way handshake
First handshake
The client calls connect to send a connection request message to the server. The packet is a special packet. The first synchronization bit SYN = 1, and the confirmation bit ACK = 0, seq = x indicates that the value of the validation field is x. The value of this field is selected by the client, the first byte Number of the data sent from the client to the server is x + 1. After the connection packet is sent, the TCP connection status of the client is changed from CLOSED to SYN_SENT.
The server calls accept to retrieve a connection request from the lisent Connection Request queue, create a socket and allocate resources for it, and start to establish a connection. The TCP connection status of the server is changed from LISENT to SYN_RCVD.
Second handshake
The server allocates resources for the connection, agrees to connect, and sends a confirmation message of SYN = 1 to the client. ACK = 1 indicates that the ack value of the validation number field is valid, and ack = x + 1 indicates that you want to receive data with the first byte number x + 1, seq = y indicates that the starting byte number of data sent from the server to the client is y + 1. The client receives confirmation, and the TCP connection status changes from SYN_SENT to ESTABLISHED.
Third handshake
The TCP connection status of the client is changed to ESTABLISHED. The confirmation message is sent to the server immediately. The connect function returns and the connection is ESTABLISHED successfully.
The server receives the confirmation message from the client. The TCP connection status is changed from SYN_RCVD to ESTABLISHED. The accept function is returned.
At this point, the three-way handshake of a connection ends. The connection is established and the client can communicate with the server reliably.
This article permanently updates the link address: