0

When I tried executing the code new InetSocketAddress("localhost", 3000); in java 13, the host is being resolved to localhost/127.0.0.1:3000 instead of resolving to localhost:3000.

Any idea why it is being resolved with the IP address of localhost (i.e. 127.0.0.1) along with the localhost in the host address?
Because of this issue, I am not able to connect to the desired server and resulting to an UnknownHostException in java.

Vibha
  • 19
  • 1
  • 1
  • 4
  • 1
    `InetSocketAddress` does not resolve into an HTTP url, so where are you actually getting that from? `InetSocketAddress` will resolve the specified hostname into an IP address, period, nothing more. So wherever you are getting `http://localhost/127.0.0.1:3000` from, that is not coming from `InetSocketAddress` itself – Remy Lebeau Oct 23 '20 at 21:10
  • Maybe related: [localhost doesn't resolve to 127.0.0.1](https://stackoverflow.com/questions/5985034/) – Remy Lebeau Oct 23 '20 at 21:15
  • Do you have the same problem if you use this instead? `new InetSocketAddress(InetAddress.getByName("localhost"), 3000);` – Remy Lebeau Oct 23 '20 at 21:17
  • Thank you Remy, I tried doing that as well. Its resolving to `localhost/127.0.0.1:3000` – Vibha Oct 23 '20 at 21:26
  • 1
    Where are you getting the information that it is being resolved to `localhost/127.0.0.1:3000`? What tool is telling you that? – President James K. Polk Oct 23 '20 at 21:31
  • I am using Java API `InetSocketAddress` to resolve to a hostname. I can see the member variable `holder` of `InetSocketAddress` of type `InetSocketAddressHolder` has an `addr` member variable of type `Inet4Address` which is being set to the hostname. – Vibha Oct 23 '20 at 21:39
  • 1
    @Vibha See WHERE exactly? WHAT are you using to look at that? You are looking at an *internal implementation* and expecting it to make sense? Unless `InetSocketAddress.toString()`, `InetSocketAddress.getHostName()`, and `InetSocketAddress.getAddress().(getHostName|getHostAddress)()` are returning the literal string `"localhost/127.0.0.1"` (which, frankly, should be impossible given how `Inet4Address` actually works), then I wouldn't worry about what is being held by the `InetSocketAddress`'s internals. – Remy Lebeau Oct 23 '20 at 21:55
  • `localhost/127.0.0.1:3000` is the result of calling `toString()` on an `InetSocketAddress`. Solution: don't. – user207421 Oct 24 '20 at 07:52

0 Answers0