TCP is the Transport layer protocol in the Internet, using three-time handshake protocols to establish a connection. When the active party sends a SYN connection request, wait for the other person to answer syn+ack[1], and eventually perform an ACK acknowledgment on the other's syn. This method of establishing a connection prevents the wrong connection from being generated. [1] The process of the TCP three-time handshake is as follows: The client sends a SYN (SEQ=X) message to the server side and enters the Syn_send state. The server receives a SYN message, responds to a SYN (seq=y) ACK (ack=x+1) message, and enters the SYN_RECV state. The client receives a server-side SYN message, responds to an ACK (ack=y+1) message, and enters the established state. Three times the handshake is complete, the TCP client and server side successfully establish the connection and can begin transmitting data. Three-time handshake for TCP
Establishing a connection requires three handshakes, while terminating a connection takes four handshakes, which is caused by a semi-shutdown of TCP (Half-close). (1) An application process first calls close, which is said to perform "active close" on that side. TCP on that side then sends a FIN section indicating that the data has been sent. (2) The peer of the fin is received to perform a "passive shutdown" (passive close), which is confirmed by TCP. Note: The receive of fin is also passed as a file terminator (End-of-file) to the receive end application process, placed after any other data that has been queued for the application process, because the receipt of fin means that the receiving application process has no additional data to receive on the corresponding connection. (3) After a period of time, the application process that receives this file terminator will call close to close its socket. This causes its TCP to also send a fin. (4) The original sender TCP receiving this final fin (i.e. the end of the active shutdown) confirms the fin. [1] Since each direction requires a fin and an ACK, it usually requires 4 sub-sections. Note: (1) "Usually" means that, in some cases, the fin of step 1 is sent along with the data, and the sections sent by step 2 and step 3 are from the end of the passive shutdown and may be merged into a sub-section. [2] (2) Between step 2 and step 3, from the execution of the passive closed end to the execution of the active closed one end of the flow of data is possible, this is called "semi-closed" (half-close). (3) When a UNIX process terminates either voluntarily (calling exit or returning from the main function) or involuntarily (receiving a signal terminating the process), all open descriptors are closed, which also causes a fin to be emitted on any TCP connection that is still open. Either the client or the server can perform an active shutdown on either side. Typically, the client performs an active shutdown, but some protocols, such as http/1.0, are actively shut down by the server. [2] Four waves of TCP
Python network programming three times handshake and four waves