Zwlin's Blog

TCP State Transition


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


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。


TCP/IP Illustrated, Volume 1 The Protocol - Kevin R. Fall