user3563385
user3563385

Reputation: 19

InetAddress.getByName("127.0.0.1.") is throwing error in linux but not in windows

I am expecting UnknownHostException when I call InetAddress.getByname("127.0.0.1.") due to extra dot in the end.

But it is not happening in windows, where as in Linux it is throwing error as expected.

Upvotes: 1

Views: 657

Answers (1)

Stephen C
Stephen C

Reputation: 718788

This is a tricky one. I don't have an answer (yet), but here is what I have found.

UPDATE - I misread the code. Based on your comment there is a native method involved, and that means platform specific code.

I checked out the OpenJDK 8 code and took a look at the native method implementations:

jdk8u/jdk/src/{windows,solaris}/native/java/net/Inet4AddressImpl.c

The two versions of the code are very different, but in both cases, an attempt would be made to resolve a malformed IP address using the host C libraries. The difference in behaviour presumably lies in the C libraries. But you can check this all for yourself by looking at the relevant version of the OpenJDK codebase.


Either way, the simple solution is probably to just fix the bad IP address. (I suppose, you could implement your own Java-side DNS service provider that specifically checked for malformed IP addresses before doing the DNS lookup, but it probably isn't worth the effort.)

Upvotes: 1

Related Questions