I use netstat
to check my port status.
I was wondering what's the difference between port status LISTENING
, TIME_WAIT
, CLOSE_WAIT
, FIN_WAIT1
and ESTABLISHED
?
I use netstat
to check my port status.
I was wondering what's the difference between port status LISTENING
, TIME_WAIT
, CLOSE_WAIT
, FIN_WAIT1
and ESTABLISHED
?
The manpage of
netstat
has a brief description of each state:You can use the state transition diagrams (examples here, here and here) to make better sense of the states.
Consider two programs attempting a socket connection (call them
a
andb
). Both set up sockets and transition to theLISTEN
state. Then one program (saya
) tries to connect to the other (b
).a
sends a request and enters theSYN_SENT
state, andb
receives the request and enters theSYN_RECV
state. Whenb
acknowledges the request, they enter theESTABLISHED
state, and do their business. Now a couple of things can happen:a
wishes to close the connection, and entersFIN_WAIT1
.b
receives theFIN
request, sends anACK
(thena
entersFIN_WAIT2
), entersCLOSE_WAIT
, tellsa
it is closing down and the entersLAST_ACK
. Oncea
acknowledges this (and entersTIME_WAIT
),b
entersCLOSE
.a
waits a bit to see if anythings is left, then entersCLOSE
.a
andb
have finished their business and decide to close the connection (simultaneous closing). Whena
is inFIN_WAIT
, and instead of receiving anACK
fromb
, it receives aFIN
(asb
wishes to close it as well),a
entersCLOSING
. But there are still some messages to send (theACK
thata
is supposed to get for its originalFIN
), and once thisACK
arrives,a
entersTIME_WAIT
as usual.Listen: Service call os using listen() system call "waiting" for a TCP connection. but this system call does not block the server, the service will continue to call accept(), which will block the server when reading an empty queue. after the server calls the listen(), the os will go through the TCP three-way handshake protocol and put an estimated connection into the queue. at this time, the server will wake up from accept() and get a new socket file descriptor for the connection. you can use the new socket fd to read from os queue. at this time your netstate get into "ESTABLISHED"