这是正常的TCP释放连接过程。其中,最不容易让人理解的地方就是时间等待(TIME-WAIT)状态。从上图可以看到,主动关闭连接的一方会经历时间等待状态,并且TIME-WAIT状态要持续2MSL(最长报文段寿命,Maximum Segment Life),之后才能进入CLOSED状态。那为什么主动关闭连接的一方要处于TIME-WAIT状态并等待2MSL时间呢?
1、为了保证A发送的最后一个ACK报文段能够到达B。这个ACK报文段有可能丢失,因而是处于LAST-ACK状态的B收不到对已发送的FIN+ACK报文段的确认。B会超时重传这个FIN+ACK报文段,那么A就能在2MSL时间内收到这个重传的FIN+ACK报文段。接着A重传一次确认,重新启动2MSL计时器。最后A和B都正常进入到CLOSED状态。
2、为了保证已经失效的重复报文段(old duplicate segments)都从网络中消失。A在发送完最后一个ACK报文段后,再经过2MSL,就可以使在本连接持续的时间内所产生的所有报文段都从网络中消失。这样就可以使下一个新的连接(源IP、源端口、目的IP、目的端口完全一样)中不会出现这种旧的报文段。
To understand the second reason for the TIME_WAIT state, assume we have a TCP connection between 12.106.32.254 port 1500 and 206.168.112.219 port 21. This connection is closed and then sometime later, we establish another connection between the same IP addresses and ports: 12.106.32.254 port 1500 and 206.168.112.219 port 21. This latter connection is called an incarnation of the previous connection since the IP addresses and ports are the same. TCP must prevent old duplicates from a connection from reappearing at some later time and being misinterpreted as belonging to a new incarnation of the same connection. To do this, TCP will not initiate a new incarnation of a connection that is currently in the TIME_WAIT state. By enforcing this rule, we are guaranteed that when we successfully establish a TCP connection, all old duplicates from previous incarnations of the connection have expired in the network.