TCP connection management. This is the basic workflow of our agreement. The connection is established, including three handshakes. The three-way handshake is used to establish a connection in the TCP protocol. To establish a connection, one party, such as the server, passively waits for a connection request to arrive by executing the LISTEN and ACCEPT primitives.
The other party, such as the customer, executes the CONNECT primitive and specifies the IP address and port number it wants to CONNECT to, sets the maximum value of TCP datagram that it can accept, and some optional user data. The CONNECT primitive sends data with SYN = 1, ACK = 0 to the target end and waits for the response from the target end.
After the datagram arrives at the destination end, the TCP entity will check whether a process is listening to the port specified by the destination port field. If no, it will send a response with RST = 1 and refuse to establish the connection.
If a process is listening on the port, the TCP datagram is sent to the process, which can accept or reject the connection. If yes, a confirmation datagram is returned. Generally, the TCP connection is established.
To release the connection, each party can send a TCP datagram with FIN = 1, indicating that this party has no data to send. When the FIN datagram is confirmed, the connection in that direction is closed. When the connection is closed in both directions, the connection is completely released. In general, releasing a connection requires four TCP datagram packets: each direction has one FIN datagram and one ACK datagram.