Skip to Main Content

Java User Groups


For appeals, questions and feedback, please email

How to try several IPs behind one hostname?

forumUsr forumFeb 17 2024


We have a Linux box that has localhost IP address (127.XX.XX.XX ) followed by the internal IP address (10.XXX.XXX.XX) listed in it's host file.

Server when started up is bound to the internal IP address.

Our application uses Java 11/17 HTTP Client and the request is sent asynchronously


HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.header("Content-Type", "application/json")

When our application ran on the same linux box, we found that the HttpClient library picked up 1st IP address (127.XXX.XXX.XX) and threw ClosedChannelException.

Is there a way to tell Java's HTTPClient to try all assigned IP addresses or if there are any best practices to follow in scenarios where a host could potentially have multiple IP address and a Java application using JDK's HTTPClient needs to connect to such a host.

BTW, I rather use Java's inbuilt libs and also I really don't want to modify our codebase at this point to use Apache's HTTPClient.

Please advice.

Thank you.

Post Details
Added on Feb 17 2024