TCP state transition diagram

1. State transition diagram

Note: Thick arrows represent typical client behavior; dashed arrows represent typical server behavior.

2. Thinking about related issues

2.1 Is 2-way handshake OK?

That's why A has to send an acknowledgment. In order to prevent the failed connection request message from being suddenly transmitted to B, an error occurs. Assuming an exception, the request sent by A did not reach B in time due to network congestion, and then retransmitted the request, then B responded, established a connection, and then released the connection. At this time, it is assumed that the first request sent by A reaches B, and B mistakenly thinks that A requests the connection again, and B establishes the connection. If two handshakes are used, the connection is established at this time, and A does not send data, which wastes the resources of B.

2.2 Why does TCP wave 4 times?

After the data transmission is over, both parties in the communication can release the connection. Now both A and B are in the ESTABLISHED state, A's application process sends a connection release segment to its TCP, and actively closes the TCP connection. A enters the FIN_WAIT1 (termination wait 1) state. Then B confirms, and B enters the CLOSE_WAIT (close wait) state. At this time, TCP is in a half-closed state, and A has no data to send. If B still wants to send data, B still receives it. After receiving the confirmation from B, A enters the FIN_WAIT2 (termination waiting 2) state, waiting for B to send a connection release message. If B has no data to send to A, B sends a request release message, and B enters the LAST_ACK (last acknowledgment) stage, waiting for A's acknowledgment. After receiving the request from B, A will issue a confirmation and then enter the TIME_WAIT (time waiting) state. At this time, the connection has not been released, and it must wait for 2MSL of the time set by the timer before A enters the CLOSED state.
        Why wait for a TIME_WAIT time at the end? One: In order to ensure that the last ACK can reach B and prevent it from being lost, B retransmits, and A cannot reply to confirm. The second is to prevent the previously mentioned "invalid connection request segment" from appearing in the connection. After A sends the last ACK, and after a time of 2MSL, all request messages generated by this connection can disappear from the network. .

2.3 Is it OK to close a TCP connection by waving 3 times?

Can. After receiving the first fin message, it does not need to transmit data by itself. It responds to the fin and ack messages, and the other party responds to the ack again, a total of three times, and the wave is completed. There are many such situations when capturing packets in practice.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325000662&siteId=291194637