If I open and close a socket by calling for instance
Socket s = new Socket( ... ); s.setReuseAddress(true); in = s.getInputStream(); ... in.close(); s.close();
Linux states that this socket is still open or at least the file descriptor for the connection is presen. When querying the open files for this process by lsof, there is an entry for the closed connection:
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME java 9268 user 5u sock 0,4 93417 can't identify protocol
This entry remains until the program is closed. Is there any other way to finally close the socket? I’m a little worried that my java application may block to many file descriptors. Is this possible? Or does java keep these sockets to re-use them even is ReuseAdress is set?
If those sockets are all in the TIME_WAIT state, this is normal, at least for a little while. Check that with netstat; it is common for sockets to hang around for a few minutes to ensure that straggling data from the socket is successfully thrown away before reusing the port for a new socket.