I'm reading from a Java Socket in a loop.
I want to re-open the socket if a read throws an exception.
In an attempt to test that, I simply close the socket and catch the exception.
The catch is, when I attempt to re-open the socket:
serverSocket = new ServerSocket(port)
socket = serverSocket.accept()
it throws an exception:
java.net.BindException: The socket name is already in use
That's an immediate problem for testing. But it might also occur in production.
How can I reliably recover, by opening a new socket connection, after an exception has been thrown?