Server Socket Leaks on stopping Java Processes

I have a strange problem on Windows 2000 Advanced Server with JDK1.5.10. Our application runs on a Tomcat 5.5.12 and is opening 2 server sockets along with Tomcat's own 8080 port. Also we deployed a stand alone application that also opens a server socket. Whenever we stop Tomcat service or our Application service (Windows services) these server sockets are not getting closed. If we use "netstat" to check if any sockets are still open it is NOT showing any sockets.

But when we use "netstat -a" command it is showing entries like

TCP10.40.1.162:808010.40.1.162:0 LISTENING

TCP10.40.1.162:2222210.40.1.162:0 LISTENING

TCP10.40.1.162:3333410.40.1.162:0 LISTENING

Here 8080 (tomcat), 22222, 33334 are the ports on which the server sockets are opened earlier. We check in Task Manager but there is no java process running. If we try to restart the same java process it is throwing JVM_Bind exception. If we reboot the box the we are able to restart the services. Similar behaviour we observed sometimes on Linux also. I guess some settings in the OS we need to tweak to fix this issue. Can somebody please help me what are those settings for Windows and Linux platforms which reclaims unused server sockets faster?

[1255 byte] By [bhaktavatsalama] at [2007-11-27 2:19:58]
# 1

Are you sure that netstat is showing states as LISTENING? not TIME_WAIT? If LISTENING the sockets are still open; the application is still running. If TIME_WAIT, try setReuseAddress(true) on your ServerSockets allowing them to bind. Tomcat should have a config param for this, too. But, this should all go away if the services shutdown cleanly (and you're closing your sockets).

developer_jbsa at 2007-7-12 2:20:54 > top of Java-index,Core,Core APIs...
# 2

The state is LISTENING not TIME_WAIT. There is a cleanly closure of sockets provided in the code. It happens to Tomcat's own ports also. If you telnet to these sockets from another box, it is connecting and staying instead of "Connection Failed" error. But in Task Manager or "Services.msc" Console no service or process running! One more observation is that there is heavy load of threads and a huge number of clients socktets were interacting with these sockets just before shutdown.

bhaktavatsalama at 2007-7-12 2:20:54 > top of Java-index,Core,Core APIs...
# 3
There is a bug in netstat whereby sockets that have been accepted and are now in TIME_WAIT_[12] are also reported as LISTENING. Could that be it?
ejpa at 2007-7-12 2:20:54 > top of Java-index,Core,Core APIs...
# 4

It is happening in other boxes. But here is a catch. If you stop service and without pausing immediately starts the service or click on "|>" restart button (which will not give any wait in between) this happens on every box. But on this particular box(Windows 2000 Server), this wait period is very large and the OS is not cleaning the socket immediately after the process is died. I just want to know which parameter in registry we need to tweak to reduce this clean up time.

bhaktavatsalama at 2007-7-12 2:20:54 > top of Java-index,Core,Core APIs...
# 5

The bug I referred to is in Windows netstat only. But try this:

serverSocket = new ServerSocket();

serverSocket.setReuseAddress(true);

serverSocket.bind(new InetSocketAddress(port));

ejpa at 2007-7-12 2:20:54 > top of Java-index,Core,Core APIs...