Hi all

I have a UDP server and many UDP clients (for hardware reasons, UDP is the only choice).

The UDP server accepts an incoming UDP connection on a default port and then assigns a new port to the client. It then starts a new thread (handler) with the new port to handle the client.

The client connects to the handler thread with the new port.

The client constantly feeds data to the handler thread.

The problem comes when I am trying to manage what will happen when the client goes down for whatever reason and then comes back up again.

I catch a datagramsocket timeout exception and have tried various ways to terminate the handling thread.

The problem is that when the client is back up again, using the same port (must use the same port assigned to it), a new handler thread i presume is started by the server and I get a java.net.BindException: Address is already in use: bind....

I really don't understand how to unbind the datagramsocket in any different way. The data flow is gone, the socket times out, the thread is stopped.

Why is the socket still bound / in use ?

What can I do about it?

I tried to use :

DatagramSocket socket = new DatagramSocket(null);
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(localAddress, localPort));

but when I use that, I get even more unexplained behavior where I get a socket timeout exception even though the client is feeding data normally..
On that front, java is picking up my VM nic IP addresses and does not pick up my normal IP address, which is weird... I have to add that manually if I am using the above way..

Anybody has any suggestions .. I am really stuck...