Java server does not release the connection lock

I have a problem very similar to jbu bind errors in Java .

The difference is that my server opens a stream to accept connections all day and then I have a script to kill the service in the morning, wait 10 seconds and then restart the service to accept connections. It works most of the time, but sometimes it will suffer BindExceptions when trying to start the service. I can't think of a good way to close a thread in a program before it gets heavily killed by an external script, so I wanted to know what would be a good way to release port locks, either from the outside, or if I have to redesign so that the service kills itself. but ensures that all connections are closed before then. (I am running Windows Server 2008 on a machine.)

0


source to share


1 answer


When a TCP connection is closed, the connection can remain in a timeout state for a period of time after the connection is closed (commonly referred to as TIME_WAIT state or 2MSL wait state). For applications using a well-known socket address or port, it may not be possible to bind the socket to the required SocketAddress if there is a connection in a timeout state to the socket address or port.
This is why you notice that sometimes the service will suffer from BindExceptions even if you wait 10 seconds.

Enabling SO_REUSEADDR before binding the socket using bind (SocketAddress) allows the socket to bind even if the previous connection timed out. This can be achieved with:



ServerSocket.setReuseAddress (true) before socket binding.
This provides or transfers the OS to reuse the same address, even if it already appears as associated ...

+1


source







All Articles