1, TCP to establish a connection of three times handshake process
The TCP session is initialized by a three-time handshake. The goal of the three-time handshake is to synchronize the sending and receiving of data segments. It also indicates to other hosts the amount of data (window size) that it can receive at one time, and establishes a logical connection. The three-time handshake can be summarized as follows:
(1) The source host sends a synchronous flag bit (SYN) 1 TCP data segment. The initial sequence number (Initial Sequence number,isn) is also indicated in this paragraph. Isn is a random value that changes over time. (Client A sends a SYN packet (SYN=J) to Server B and enters the syn_send state, waiting for Server B to confirm.) )
(2) The target host sends back the confirmation data segment, the synchronization flag bit (SYN) in this paragraph is also set to 1, and the confirmation flag bit (ACK) also set 1, while the confirmation ordinal field indicates that the target host expects to receive the sequence number of the next data segment of the source host (that is, the previous data segment has received and there is no In addition, the segment initial sequence number of the target host is also included in this paragraph. (Server B receives the SYN packet, must confirm the SYN (ACK=J+1) of customer A, and also sends a SYN packet (SYN=K), which is the Syn+ack packet, at which point Server B enters the SYN_RECV state. )
(3) The source host sends back a data segment, with the same increment of the sending sequence number and confirmation sequence number. (Client A receives Server B's Syn+ack package, sends acknowledgment packet ACK (ack=k+1) to Server B, and the packet is sent, and client A and server B enter the established state to complete the three handshake.) )
So far, the three-time handshake for the TCP session is complete. Next, the source host and target host can send and receive data to each other. The entire process can be represented in Figure 2-8.
2. Four handshake process for TCP release connection
(1) Client A sends a fin to turn off customer A to Server B data Transfer (message segment 4).
(2) Server B receives this fin, which sends back an ACK, confirming that the serial number is received plus 1 (message Segment 5). As with Syn, a fin will occupy a sequence number.
(3) Server B closes the connection to client A and sends a fin to client a (message segment 6).
(4) Client A sends back ACK message confirmation, and sets the confirmation serial number to receive the serial number plus 1 (message segment 7).
TCP uses four waves to close the connection as shown in Figure 2
Figure 2 TCP Four waves to close the connection
Release of TCP connection:
Although TCP connections are full-duplex, in order to understand the release process of TCP connections, it is best to consider a TCP connection as a pair of simplex connections. Each single-work connection is released separately, and two single-work connections are independent of one another. To release a connection, either party can send a TCP data segment that has a fin bit set, which means it has no data to send. When the fin data segment is confirmed, it stops transmitting new data in this direction. However, it is possible that the data will continue to be transmitted indefinitely in another direction. When all two directions are stopped, the connection is released. Typically, in order to release a connection, 4 TCP data segments are required: a fin and an ack in each direction. However, the first ACK and the second fin are likely to be included in the same data segment, reducing the total to 3.
If a fin's response is not reached within the twice-fold maximum packet lifetime, the sender of fin will release the connection directly. The other side will eventually notice that it seems as if the other party is no longer listening to the connection and will also time out. Although imperfect in theory, there are few problems in practice.
1. Why is a connection agreement a three-time handshake, while closing the connection is a four-time handshake?
This is because the socket in the listen state of the server is sent in a message after it receives a request for the connection of the SYN message, and it can put the ACK and SYN (ACK response, and SYN synchronous). However, when the connection is closed, when receiving the other's fin message notification, it simply means that no data is sent to you, but not all of your data are sent to the other side, so you may not immediately close the socket, that is, you may also need to send some data to the other side, Send the FIN message to the other side to show that you agree that you can now close the connection, so it is here that the ACK message and fin messages are sent separately in most cases.
2. Why does the TIME_WAIT state still need to wait 2MSL before returning to the closed state.
This is because although both sides agree to close the connection, and the handshake of the 4 messages are also coordinated and sent, it can be directly back to the closed state (as from the Syn_send state to the establish state); but because we have to assume that the network is unreliable, You cannot guarantee that the last ACK message you send will be received by the other party, so the socket in the Last_ack state may be re-sending the fin message because the timeout does not receive an ACK message, so this time_wait state is used to resend the possible missing ACK message.