Search code examples
socketstcpserversockettime-wait

ServerSocket open and immediate close on localhost will lead to TIME_WAIT?


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?


Solution

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