c++socketsnetwork-programmingtcpnetstat

Socket programming, what about "CLOSE_WAIT", "FIN_WAIT_2" and "LISTENING"?


I am writing a socket based C application which seems to behave in a very unstable way. The code is standard socket handling on TCP port 6683, and I know it has worked before. Instead of mentioning the source code, I believe the most interesting are the results of the netstat -aon command:

When it worked fine, the results of the netstat -aon| grep 6683 command were:

TCP    127.0.0.1:6683         127.0.0.1:50888        CLOSE_WAIT      6128
TCP    127.0.0.1:50888        127.0.0.1:6683         FIN_WAIT_2      3764

When it does not work anymore, the results of the netstat -aon | grep 6683 command are:

TCP    127.0.0.1:6683         0.0.0.0:0              LISTENING       7800

Does anybody know the meaning of the mentioned netstat results, what this might mean for the socket handling and what can I do in order to return to the situation giving the first results?

Thanks


Solution

  • From the netstat documentation :

    FIN_WAIT2 Connection is closed, and the socket is waiting for a shutdown from the remote end.

    CLOSE_WAIT The remote end has shut down, waiting for the socket to close.

    The LISTENING state is just the server socket waiting for clients. This is a normal behavior for a listening server socket (which is not the same as the connection server socket).

    You can see that the side with FIN_WAIT2 is closed and waiting for the other, but the side with CLOSE_WAIT, is currently closing, but not yet closed. Based on the LISTENING socket, the client is closed, and the currently closing side is the server. The server is probably waiting because there is data to read that was not yet read. It can't close the socket without data loss, which is unacceptable for TCP. The connection should close normally after all the data left on the server side is read.