If I have a simple server that starts like this.
ServerSocket serverSocket = new ServerSocket(PORT_NUMBER);
And a simple client that connects to it:
socket = new Socket("host", 5081);
The host is a string and can be a value like this: "192.168.1.4"
If there is no service with the host "192.168.1.4" it causes java.net.ConnectException
Is there a way to prevent the exception and allow for loop that iterates over an array of hosts, and breaks once reached a valid connection, and continue to exception only if the array of hosts has been exhausted without a connection.
Yes, you need to use a try...catch
inside a loop.
You can store the exception in a variable and only re-throw it from that variable if you hit the end of the loop without success.