_jvm BindException

I am currently developing a simple server application to be used in combination with a couple of client computers running applets. Things work pretty well, except for one problem. If an exception occurs somewhere along the way, and the system exits abnormally, then the next time I start the server, I get a jvm BindException since the old ServerSocket is still listening on the port that I am using. How can I get the old objects from the old instance of the application to die?

-Arthur

[507 byte] By [arthur-g] at [2007-9-26 2:36:11]
# 1

> get a jvm BindException since the old ServerSocket

> is still listening on the port that I am using.

> How can I get the old objects from the old instance

> of the application to die?

You misunderstand what is happening. When your application exits all java objects cease to exist.

TCP sockets, which are used by java, but are not defined by java, rely on guaranteed delivery of messages. This includes handling messages that arrive after an application has shut down. So sockets can 'linger' after an application exits. This typically ranges from 2 minutes to 5 minutes.

There are two solutions:

-Catch the bind exception and keep retrying until it succeeds.

-Use the setSoLinger() method to lower the threshold.

A combination of both methods is probably best as it is not a good idea to lower threshold to low.

And also presumably your application is exiting abnormally and not normal. A normal exit should close the socket. And a checked exception should never allow the socket to remain open. (And I would try to close even with unchecked exceptions.)

jschell at 2007-6-29 10:03:37 > top of Java-index,Java HotSpot Virtual Machine,Specifications...