'Release' a listening ServerSocket in Java

java, sockets

Solution

I would not kill/interrupt the thread in which the ServerSocket is listening. Just call close on the ServerSocket from the other thread.

Problem

I have a thread (let's say it's called ConnListener) that listens for an incoming connection through a `ServerSocket`'s `accept()` method. In another thread (call it StartStopSwitch) I want the listening thread to be stopped, and no longer listen on the given address and port. I could kill the thread, using `Thread.interrupt()`, but then when I check the used ports through the `netstat` command, I still see the port with state 'LISTENING'. I want to 'release' (or 'unbind') the `ServerSocket`, without throwing an exception, so it won't show up when using `netstat`. Now how can I do that?

Original source