TCP three-way handshake

TCP is a connection-oriented protocol, so a connection must be established before either party can send data. The three-way handshake is the method used in TCP/IP networks to create a connection between localhost/client and server. This is a three-step approach that requires the client and server to exchange SYN and ACK packets before the actual data communication begins. To establish a connection, three handshakes occur:

1.SYN: The client sends a SYN to the server. The client sets the sequence number (SEQ) of the segment to A random value A. 2. Syn-ack: The server responds with syn-ACK. The acknowledgement number is set to one more than the received serial number, A+1, and the server selects another random serial number B for the packet. 3. Finally, the client sends an ACK back to the server. The serial number is set to the received acknowledgement value, that is, A+1, and the acknowledgement number is set to more than one of the received serial numbers, that is, B+1.

At this point, both the client and server have received a connection confirmation. Step 1 and step 2 Set up a connection parameter (serial number) in one direction and confirm it. Step 2 and step 3 set up connection parameters (serial numbers) for the other direction and confirm. Through these, full-duplex communication is established.

TCP waved four times

TCP is closed four times, which means that both parties need to send a FIN to tell each other I’m closing and reply fin-ACK. So one side can send data while the other side is closed, we call this phenomenon TCP half closed. Of course, the initiator can also choose not to accept. TCP four waves:

1.FIN: The active sender sends the FIN to the passive to tell the passive that I have finished sending data and want to close the connection. 2.ACK: The server receives the FIN and replies with an ACK to inform me that it has received the FIN (unlike establishing a connection, it sends an ACK with its own FIN because it may not have completed the data transfer). It can continue the incomplete data transfer and set the status to CLOSE_WAIT. After receiving an ACK, the server changes its status to FIN_WAIT2 and waits for the FIN from the passive end. 3.FIN: The server sends a FIN to the active end and waits for the ACK from the passive end to enter the LAST_ACK state. The active party receives the FIN sent by the passive party and replies with an ACK

The active party enters the TIME_WAIT state and then waits for 2MSL(Maximum segment lifetime) to enter the CLOSED state. The passive party enters the CLOSED state after receiving the ACK. Because the network environment is complex and changeable, the loss of the last ACK may cause the FIN to be retransmitted. Therefore, the initiating party should wait for 2MSL to prevent the retransmission. Finally, attached is a TCP state machine, for reference in Chapter 18 of TCP/IP Illustrated Vol. 1 (English version is strongly recommended. If English is not good, you can buy a Chinese version for reference).