TCP connection: TCP connection: TCP connection: TCP connection

An overview

TCP is a connection-oriented protocol. Transport connections are used to transmit TCP packets. The establishment and release of TCP transport connections is an essential process in every connection-oriented communication. Therefore, transport connections have three phases: connection establishment, data transfer and connection release.

There are three problems to be solved during TCP connection establishment.

(1) To make one party clearly know the existence of the other party. (2) Allow both parties to negotiate some parameters (such as the maximum window value, etc.). (3) Able to transport physical resources for distribution.

TCP connection establishment (three-way handshake)

As shown in the figure above, the TCP connection process is shown in the figure above. Assume host A is running A TCP client program and host B is running A TCP server program. Initially, TCP processes at both ends are in CLOSE state. In the figure, the states of TCP processes are shown in the boxes below the host. Note that A actively opens the link, while B passively opens the connection.

The TCP server process of B creates the TCB and is ready to accept the connection request from the client process. The server process is then in LISTEN state, waiting for a connection request from the client. If so, respond immediately.

The TCP client process of A also creates the transport control block TCB first and then sends A connection request message segment to B. This is the synchronization bit of the header SYN = 1, and an initial sequence number seq = x is selected. The TCP client enters the SYN-send state when the SEQUENCE number of a SYN packet is omitted, but the segment cannot carry data.

After receiving the connection request packet, user B agrees to connect to user A. In the acknowledgement packet segment, set the SYN bit and ACK bit to 1, ACK = x + 1, and select an initial sequence number seq = Y. Note that this message segment also does not carry any data, but again consumes a sequence number. At this point, the TCP service enters the SYN-RCVD state.

After receiving an acknowledgement from B, the TCP client process also sends an acknowledgement to B. The ACK of the packet segment is set to 1, the ACK number is Y + 1, and its serial number is seq = x + 1. According to the TCP standard, THE ACK packet segment can carry data. However, if no data is carried, the sequence number is not consumed. In this case, the sequence number of the next data packet segment is still seq = x + 1. After the TCP connection is ESTABLISHED, A enters the ESTABLISHED state. After B receives A’s confirmation, it also enters the ESTABLISHED state.

TCP connection release (four waves)

After data transfer, both communication parties can release the connection. Now both A and B are in ESTABLISHED state. The application process of A sends A connection release packet segment to its TCP, stops sending data, and actively closes the TCP connection. A sets the termination control position FIN at the head of the connection release message segment to 1, and its serial number seq = U, which is equal to the serial number of the last byte of the data transmitted in the previous process plus 1. At this time, A enters the FIN_WAIT_1 (stop wait bit 1) state and waits for B’s confirmation. Note that TCP specifies that a FIN segment consumes a sequence number even if it does not carry data.

B sends an acknowledgement immediately after receiving the packet segment released from the connection. The acknowledgement number is ACK = U + 1, and the serial number of this packet segment is V, which is equal to the last byte of the data transmitted before B plus 1. B then enters the CLOSE_WAIT state. The TCP server process notifies the higher-level application process, and the connection from A to B is released. The TCP connection is half-closed, that is, A has no data to send, but IF B sends data, A still receives it. That is, the connection from B to A is not closed, and may remain so for some time.

After receiving the acknowledgement from B, A enters the FIN_WAIT_2 (terminate wait 2) state and waits for B to send the connection-release message segment.

If B has no data to send to A, its application notifies TCP to release the connection. In this case, USER B sends a connection-release packet segment that sets FIN to 1. Let’s assume that B has serial number W (in the half-closed state, B may send some more data). B must also repeat that the confirmation number ACK = u + 1 was sent last time. Then B enters the LAST_ACK state and waits for A’s confirmation.

After receiving the connection-release segment from B, A must send an acknowledgement. Set the ACK number to 1, ACK = W + 1, and its sequence number to SEq = U + 1. (According to TCP standards, FIN packets that have been previously sent consume a sequence number.) Then enter the TIME_WAIT state. Note that the TCP connection has not yet been released. A can enter the CLOSE state only after the time set by the timer is 2MSL. The time MSL is called maximum Packet segment life, and RFC 793 recommends setting it to 2 minutes. But this is purely for engineering reasons, and MSL = 2 minutes may be too long for today’s networks. So TCP allows smaller MSL values to be used at different times depending on the situation. Therefore, after entering the TIME_WAIT state from A, it takes 4 minutes to enter the CLOSE state and establish the next connection. When A cancels the corresponding transmission control block TCB, the TCP connection is terminated.

Two little questions

Why would A send A confirmation in the middle of three handshakes? This is mainly to prevent the invalid connection request packet from suddenly being sent to B and causing an error.

Why must A wait 2MSL in TIME_WAIT state?

First, to ensure that the last ACK segment sent by A can reach B.

Second, prevent the “invalid connection request segment” just mentioned from appearing in this connection. After sending the last ACK packet segment, A can make all the packet segments generated during the connection duration disappear from the network after 2MSL. This prevents the old connection request segment from appearing in the next new connection.

Refer to the link

Computer Networking edition 6

Pay attention to our