的联机帮助页中netstat
简要说明了每种状态:
ESTABLISHED
The socket has an established connection.
SYN_SENT
The socket is actively attempting to establish a connection.
SYN_RECV
A connection request has been received from the network.
FIN_WAIT1
The socket is closed, and the connection is shutting down.
FIN_WAIT2
Connection is closed, and the socket is waiting for a shutdown
from the remote end.
TIME_WAIT
The socket is waiting after close to handle packets still in the
network.
CLOSE The socket is not being used.
CLOSE_WAIT
The remote end has shut down, waiting for the socket to close.
LAST_ACK
The remote end has shut down, and the socket is closed. Waiting
for acknowledgement.
LISTEN The socket is listening for incoming connections. Such sockets
are not included in the output unless you specify the
--listening (-l) or --all (-a) option.
CLOSING
Both sockets are shut down but we still don't have all our data
sent.
UNKNOWN
The state of the socket is unknown.
您可以使用状态转换图(此处,此处和此处的示例)更好地理解状态。
考虑两个试图进行套接字连接的程序(分别调用a
和b
)。两者都设置了套接字并过渡到该LISTEN
状态。然后,一个程序(例如a
)尝试连接到另一个程序(b
)。a
发送请求并进入SYN_SENT
状态,然后b
接收请求并进入SYN_RECV
状态。当b
确认请求后,他们进入ESTABLISHED
状态并开展业务。现在可以发生几件事:
a
希望关闭连接,然后输入FIN_WAIT1
。b
接收到FIN
请求,发送一个ACK
(然后a
进入FIN_WAIT2
),进入CLOSE_WAIT
,告诉a
它正在关闭,然后进入LAST_ACK
。一旦a
确认此(和进入TIME_WAIT
),b
进入CLOSE
。a
等一下看是否还有东西,然后输入CLOSE
。
a
并且b
已经完成他们的业务并决定关闭连接(同时关闭)。在a
中时FIN_WAIT
,而不是ACK
从中接收b
,而是接收FIN
(也b
希望将其关闭),a
进入CLOSING
。但仍有一些消息发送(在ACK
那个a
应该得到原来的FIN
),并且,一旦这ACK
到达,a
进入TIME_WAIT
照常进行。