简体   繁体   English

InetAddress.getByName(“ 127.0.0.1。”)在Linux中引发错误,但在Windows中未引发

[英]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. 由于结尾有多余的点,当我调用InetAddress.getByname("127.0.0.1.")时,我期望UnknownHostException。

But it is not happening in windows, where as in Linux it is throwing error as expected. 但是它不会在Windows中发生,因为在Linux中它会按预期抛出错误。

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. 根据您的评论,涉及一个native方法,这意味着平台特定的代码。

I checked out the OpenJDK 8 code and took a look at the native method implementations: 我签出了OpenJDK 8代码,并查看了本机方法的实现:

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. 两种版本的代码非常不同,但是在两种情况下,都将尝试使用主机C库来解析格式错误的IP地址。 The difference in behaviour presumably lies in the C libraries. 行为上的差异大概在于C库中。 But you can check this all for yourself by looking at the relevant version of the OpenJDK codebase. 但是,您可以通过查看OpenJDK代码库的相关版本来亲自检查所有内容。


Either way, the simple solution is probably to just fix the bad IP address. 无论哪种方式,简单的解决方案都可能是修复错误的IP地址。 (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.) (我想,您可以实现自己的Java端DNS服务提供者,该服务提供者在进行DNS查找之前专门检查了格式错误的IP地址,但是这样做可能不值得。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM