In the process of transmitting data using a TCP connection, the client and server need to go through a three-way handshake to establish a connection and a four-way handshake to disconnect.
The details are shown in the figure below
The above figure describes the detailed process of TCP connection from establishment to disconnection. The specific message details will be discussed below.
During the TCP three-way handshake to establish a connection, the client first sends a SYN=1, Sequence=p request packet to the server.
If the server agrees to grant the link to the client, it sends a response packet of ACK=p+1, SYN=1, Sequence=q.
Finally, after the client receives the response packet from the server, it will also send a response packet to the server. The specific content is ACK=q+1, SYN=0, Sequence=p+1.
For TCP disconnection, four handshakes are required. Why not three handshakes? The specific reasons are:
When the server receives the SYN connection request message from the client, it can directly send the SYN+ACK message . The ACK message is used for response, and the SYN message is used for synchronization. But when closing the connection, when the server receives the FIN message, it is likely that the SOCKET will not be closed immediately, so it can only reply with an ACK message first, telling the client, "I received the FIN message you sent." Only when all the messages on my server side have been sent can I send the FIN message, so it cannot be sent together. Therefore, a four-step handshake is required.
The process of disconnection is initiated by the client first. First, the client will send a disconnection request packet with FIN=1 and Sequence=m.
After receiving the request, the server will send a confirmation packetACK=m+1, Sequence=n.
At this time, the unilateral connection between the client and the server has been disconnected, and the client will no longer send data packets to the server. The server can still send data to the client. If the server does not need to send data to the client after sending s data packets, it can send a disconnect request to the client at this time.FIN=1, Sequence=m+s, ACK=m+1.
After the client receives the server’s disconnect request, it will send a confirmation packet.Sequence=m+1,ACK=m+s+1;
Note that after the client sends the last ACK packet, it will still wait for 2MSL (maximum segment survival time), because the packet may will be lost. The client waits for 2MSL because it is worried that the server does not receive the ACK packet and resends the FIN request.
The above introduces the three-way handshake and four-way dissolution process of TCP connection, including the content of TCP connection and three-way handshake. I hope it will be helpful to friends who are interested in PHP tutorials.