端口状态“ LISTENING”,“ TIME_WAIT”,“ CLOSE_WAIT”和“ ESTABLISHED”之间有什么区别?


Answers:


58

联机帮助页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.

您可以使用状态转换图(此处此处此处的示例)更好地理解状态。


考虑两个试图进行套接字连接的程序(分别调用ab)。两者都设置了套接字并过渡到该LISTEN状态。然后,一个程序(例如a)尝试连接到另一个程序(b)。a发送请求并进入SYN_SENT状态,然后b接收请求并进入SYN_RECV状态。当b确认请求后,他们进入ESTABLISHED状态并开展业务。现在可以发生几件事:

  1. a希望关闭连接,然后输入FIN_WAIT1b接收到FIN请求,发送一个ACK(然后a进入FIN_WAIT2),进入CLOSE_WAIT,告诉a它正在关闭,然后进入LAST_ACK。一旦a确认此(和进入TIME_WAIT),b进入CLOSEa等一下看是否还有东西,然后输入CLOSE
  2. a并且b已经完成他们的业务并决定关闭连接(同时关闭)。在a中时FIN_WAIT,而不是ACK从中接收b,而是接收FIN(也b希望将其关闭),a进入CLOSING。但仍有一些消息发送(在ACK那个a应该得到原来的FIN),并且,一旦这ACK到达,a进入TIME_WAIT照常进行。

穆鲁答案的其他信息。netstat的状态图(引自:http : //www4.cs.fau.de/Projects/JX/Projects/TCP/tcpstate.html)!在这里输入的形象描述
charkh
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.