Hello,

I'm working on an application that uses a ServerSocket, but it (at points) stops the server thread and restarts it, throwing a java.net.BindException: address already in use exception.

How can I unbind the ServerSocket (.close() is not working to unbind) so I can re-use the same port seconds later?

Note: The thread the ServerSocket is in is redefined when I reload it.

-- Turt2Live

Recommended Answers

An idea: Connect to the socket so the accept method returns and then have the code exit the thread.

Jump to Post

Can you make a small simple program that compiles, executes and shows the problem (a SSCCE)?

Do you close the socket?

Jump to Post

All 5 Replies

An idea: Connect to the socket so the accept method returns and then have the code exit the thread.

An idea: Connect to the socket so the accept method returns and then have the code exit the thread.

I do that, it's just the way the superclass handles (not mine) my code. The classloader loads, runs the code, then on command unloads and reloads the code. The JVM however seems to be holding onto the bind :/

Can you make a small simple program that compiles, executes and shows the problem (a SSCCE)?

Do you close the socket?

Can you make a small simple program that compiles, executes and shows the problem (a SSCCE)?

Do you close the socket?

While creating this, I found my problem :P

The classloader was not properly unloading the class.

(Bad code on their fault :P)

Sometimes its more easier to do a prototype as NormR1 said.
Nice you got it fix

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.