Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream – based transport layer communication Protocol. TCP works at the fourth layer of the OSI seven-layer network model, the transport layer. The following figure shows the OSI seven-layer model, the role of each layer and the corresponding protocol. For more exciting content, please follow my wechat official account: JavaQ.

Paste_Image.png

TCP is a transport layer protocol and uses the three-way handshake protocol to establish a connection before data transmission. The general process is that after the client sends a SYN connection request, the server replies with SYN+ACK after receiving the request, and the client replies with ACK after receiving the reply from the server. This method of establishing a connection can prevent incorrect connections. Prevents an invalid connection request segment from suddenly being sent to the server. The TCP three-way handshake is shown as follows:

Paste_Image.png

The TCP three-way handshake process is described as follows: 1. The client sends a connection request packet segment with the SYN flag bit 1 and Sequence Number X. Then the client enters the SYN_SEND state and waits for a response from the server. 2. The server receives the connection request from the client, acknowledges the SYN segment, and sends an Acknowledgment Number of X +1(Sequence Number+1), with both the SYN flag bit and ACK flag bit set to 1. If the packet segment (SYN+ACK packet segment) whose Sequence Number is Y is sent to the client, the server enters the SYN_RECV state. 3. The client receives the SYN+ACK packet from the server, and after acknowledging the ACK, sends the packet segment with the Acknowledgment Number y+1, SYN flag bit 0, and ACK bit 1. After the packet is sent, both the client and server enter the ESTABLISHED state to complete the TCP three-way handshake. The client and server have successfully established a connection and can begin transferring data.

When the data transfer is complete, the connection needs to be disconnected four times in order to complete the data transfer correctly. The TCP quad-wave process is shown as follows:

Paste_Image.png

The description of the TCP four-time wave is as follows: 1. The client sends the FIN packet segment with the Sequence Number x+2, y+1, and the client enters the FIN_WAIT_1 state, which tells the server that there is no data to be transmitted and asks to close the connection. 2. After receiving the FIN packet segment from the client, the server should answer an ACK packet segment with Sequence Number+1 to the client, that is, I have received your request but am not ready. Please wait for my shutdown request. The client enters the FIN_WAIT_2 state. 3. After data transmission, the server sends a FIN packet with Sequence Number Y +1 to the client to close the connection. The server enters the LAST_ACK state. 4. After receiving the FIN packet segment from the server, the client sends an ACK packet segment with the Sequence Number+1 to the server, and the client enters the TIME_WAIT state. After receiving the ACK message segment from the client, the server closes the connection and enters the CLOSED state. If the client still does not receive any reply after 2MSL, it indicates that the server is normally CLOSED and the client closes the connection and enters the CLOSED state.