0

I am seeing following logic in one of the java libs to test open of socket on localhost:

 ServerSocket socket = new ServerSocket(port);
 socket.close();

My question is would this socket lead to TIME_WAIT state on localhost when no data packets have been sent and socket is closed immediately after opening it? In such a case if the application attempts to bind to same port can it cause 'address already in use error' if done within 2MSL value?

I wrote a small test program like above, but when I netstat or ss on linux machine where I ran this program, I do not see TIME_WAIT at all for this port. Shouldn't the state machine apply even in case the socket has not been used to send any data packets?

Sumit Nigam
  • 255
  • 3
  • 13
  • If the socket is never moved out of the `CLOSED` state, it doesn't have to do anything to return to the `CLOSED` state. – Damien_The_Unbeliever Oct 18 '16 at 10:23
  • Hi @Damien_The_Unbeliever - Sorry, I did not get the point. Would the socket in case above go through TIME_WAIT? – Sumit Nigam Oct 18 '16 at 10:26
  • 2
    I'm not sure which specific state machine diagram you're looking at, but say [this one](http://www.tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF-2.htm). The socket *starts* in a `CLOSED` state. It then has to go through several specific transitions to become `ESTABLISHED`, and then has to go down one particular route to end up waiting in `TIME_WAIT` before returning to `CLOSED`. You've not done anything to move your socket out of the `CLOSED` state, so no transitions are needed. – Damien_The_Unbeliever Oct 18 '16 at 10:39
  • Thank you for your reply. If I add a sleep in between creating the serversocket and closing, and try sending any data to the port using nc in that interval, then netstat now shows connection in FIN_WAIT2 state for the nc process while for the program is in LISTEN state. Again, I understand that I have not written any code to accept incoming connection but as per the diagram above, it should reach TIME_WAIT from FIN_WAIT2. However, I do not see it reaching that state but it just goes away. Or likely, it is closed with RST flag. – Sumit Nigam Oct 18 '16 at 13:51

1 Answers1

1

TIME_WAIT is a state in a connection. You haven't created a connection, ergo you never reach TIME_WAIT.

user207421
  • 305,947
  • 44
  • 307
  • 483