Document directory
- In TCP/IP, TCP provides reliable connection services and uses three handshakes to establish a connection, as shown in figure 1.
In TCP/IP, TCP provides reliable connection services and uses three handshakes to establish a connection, as shown in figure 1.
(1) first handshake: when a connection is established, client a sends the SYN Packet (SYN = J) to server B and enters the syn_send status, waiting for confirmation from server B.
(2) second handshake: When server B receives the SYN packet, it must confirm the Syn (ACK = J + 1) of Customer A and send a SYN Packet (SYN = k) by itself ), that is, the SYN + ACK packet. At this time, server B enters the syn_recv state.
(3) third handshake: Client A receives the SYN + ACK package from server B and sends a confirmation package ack (ACK = k + 1) to server B. The package is sent completely, client A and server B enter the established status and complete three handshakes.
After three handshakes are completed, the client and the server start to transmit data.
Figure 1 TCP three-way handshake
Because the TCP connection is full-duplex, each direction must be closed separately. This principle is that when one party completes its data sending task, it can send a fin to terminate the connection in this direction. Receiving a fin only means that there is no data flow between the two parties. a tcp connection can still send data after receiving a fin. First, the party that closes the service will take the initiative to close the service, and the other party will passively close the service.
(1) Client A sends a fin to disable data transmission from client a to server B (packet segment 4 ).
(2) When server B receives the fin, it sends back an ACK and confirms that the serial number is 1 (packet segment 5 ). Like SYN, a fin occupies a sequence number.
(3) server B Closes the connection with client a and sends a fin to Client A (packet segment 6 ).
(4) Client A sends back the ACK message for confirmation, and sets the confirmation sequence number to receive the serial number plus 1 (packet segment 7 ).
TCP uses four waves to close the connection 2.
Figure 2 TCP four-click to close the connection
1. Why is the three-way handshake while the four-way handshake when the connection is closed?
This is because the socket in the listen status of the server can respond to ack and SYN after receiving the SYN Packet connection request) it is sent in a message. However, when the connection is closed, when the other party receives the fin Message notification, it only indicates that the other party has no data to send to you; but not all your data may have been sent to the other party, therefore, you may not close the socket immediately, that is, you may need to send some data to the other party, and then send the fin message to the other party to indicate that you agree to close the connection now, therefore, the ACK messages and fin messages are sent separately in most cases.
2. Why does the time_wait status still need to be 2 MSL before it can be returned to the closed status?
This is because both parties have agreed to close the connection, and the four handshake packets are also coordinated and sent, it can be directly returned to the closed state (just like from syn_send to establish State), but because we must assume that the network is unreliable, you cannot guarantee that the last ACK message you sent will be received by the other party. Therefore, the socket in the last_ack status of the other party may fail to receive the ACK message due to timeout, but resend the fin message, therefore, the time_wait status is used to resend potentially lost ACK packets.
(1) first handshake: when a connection is established, client a sends the SYN Packet (SYN = J) to server B and enters the syn_send status, waiting for confirmation from server B.
(2) second handshake: When server B receives the SYN packet, it must confirm the Syn (ACK = J + 1) of Customer A and send a SYN Packet (SYN = k) by itself ), that is, the SYN + ACK packet. At this time, server B enters the syn_recv state.
(3) third handshake: Client A receives the SYN + ACK package from server B and sends a confirmation package ack (ACK = k + 1) to server B. The package is sent completely, client A and server B enter the established status and complete three handshakes.
After three handshakes are completed, the client and the server start to transmit data.
Figure 1 TCP three-way handshake
Because the TCP connection is full-duplex, each direction must be closed separately. This principle is that when one party completes its data sending task, it can send a fin to terminate the connection in this direction. Receiving a fin only means that there is no data flow between the two parties. a tcp connection can still send data after receiving a fin. First, the party that closes the service will take the initiative to close the service, and the other party will passively close the service.
(1) Client A sends a fin to disable data transmission from client a to server B (packet segment 4 ).
(2) When server B receives the fin, it sends back an ACK and confirms that the serial number is 1 (packet segment 5 ). Like SYN, a fin occupies a sequence number.
(3) server B Closes the connection with client a and sends a fin to Client A (packet segment 6 ).
(4) Client A sends back the ACK message for confirmation, and sets the confirmation sequence number to receive the serial number plus 1 (packet segment 7 ).
TCP uses four waves to close the connection 2.
Figure 2 TCP four-click to close the connection
1. Why is the three-way handshake while the four-way handshake when the connection is closed?
This is because the socket in the listen status of the server can respond to ack and SYN after receiving the SYN Packet connection request) it is sent in a message. However, when the connection is closed, when the other party receives the fin Message notification, it only indicates that the other party has no data to send to you; but not all your data may have been sent to the other party, therefore, you may not close the socket immediately, that is, you may need to send some data to the other party, and then send the fin message to the other party to indicate that you agree to close the connection now, therefore, the ACK messages and fin messages are sent separately in most cases.
2. Why does the time_wait status still need to be 2 MSL before it can be returned to the closed status?
This is because both parties have agreed to close the connection, and the four handshake packets are also coordinated and sent, it can be directly returned to the closed state (just like from syn_send to establish State), but because we must assume that the network is unreliable, you cannot guarantee that the last ACK message you sent will be received by the other party. Therefore, the socket in the last_ack status of the other party may fail to receive the ACK message due to timeout, but resend the fin message, therefore, the time_wait status is used to resend potentially lost ACK packets.