Zwlin's Blog

TCP State Transition

Last updated: 2021/04/01     Published at: 2021/04/01

TCP 的状态转换非常经典,最近学习了 TCP/IP Illustrate Volume I Chapter 13 中关于这部分的内容,在此记录一下。

TCP State Transition Diagram

TCP 状态转换图

TIME_WAIT (2MSL Wait) State

1sysctl -a | grep tcp_fin_timeout
2net.ipv4.tcp_fin_timeout = 60

2MSL 的意义:

FIN_WAIT_2 State

当主动关闭连接的那一方发送完它的 FIN 并且收到对端的 ACK 之后,会进入 FIN_WAIT_2 状态,这时候,如果收到对端的 FIN,才会进入到 TIME_WAIT 状态,但是,存在一种情况就是对端一直不发送 FIN,就会导致自身一直停留在 FIN_WAIT_2,而对端一直停留在 CLOSE_WAIT,直到对端决定关闭连接。

Many implementations prevent this infinite wait in the FIN_WAIT_2 state as follows: If the application that does the active close does a complete close,not a half-close indicating that it expects to receive data, a timer is set.**If the connection is idle when the timer expires, TCP moves the connection into the CLOSED state.**In Linux, the variable net.ipv4.tcp_fin_timeout can be adjusted to control the number of seconds to which the timer is set. Its default value is 60s.

Simultaneous Close

For a simultaneous close, in terms of Figure, both ends go from ESTABLISHED to FIN_WAIT_1 when the application issues the close. This causes both FINs to be sent, and they probably pass each other somewhere in the network. When its peer’s FIN arrives, each end transitions from FIN_WAIT_1 to the CLOS- ING state, and each endpoint sends its final ACK.Upon receiving a final ACK, each endpoint’s state changes to TIME_WAIT, and the 2MSL wait is initiated.

TCP 同时关闭的时候,两端都会进入 TIME_WAIT,并且要等待 2MSL。