Linux 当主机名是 64 个字符时 InetAddress.getLocalHost() 返回错误结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4871451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InetAddress.getLocalHost() returns wrong result when hostname is 64 chars
提问by Nishan
I am using below code to print out hostname of a linux box using java 1.5
我正在使用以下代码使用 java 1.5 打印出 linux 机器的主机名
public static void main(String a[]) {
System.out.println( InetAddress.getLocalHost().getCanonicalHostName() );
}
When I have hostname of the system a 64 char length string, the code just prints 'localhost.localdomain'. If my hostname length is less than 64, it prints out the hostname correctly. The max hostname length for the system is 64 (getconf HOST_NAME_MAX gives 64)
当我的系统主机名是一个 64 个字符长度的字符串时,代码只打印“localhost.localdomain”。如果我的主机名长度小于 64,它会正确打印出主机名。系统的最大主机名长度为 64(getconf HOST_NAME_MAX 给出 64)
What could be wrong here? Could this be a bug (though, I am inclined to think problem is on my side)
这里可能有什么问题?这可能是一个错误(不过,我倾向于认为问题在我这边)
Thanks for help!
感谢帮助!
回答by earldouglas
It's hard to guess what might be going wrong in your case, but based on the corresponding code from Java 6, it could be as simple as a name resolution problem, or possibly Java falsely thinking that your 64 character hostname is spoofed.
很难猜测您的情况可能出了什么问题,但根据Java 6 中的相应代码,它可能像名称解析问题一样简单,或者 Java 可能错误地认为您的 64 个字符主机名是欺骗性的。
回答by e2-e4
What is likely to happen on Linux is that InetAddress.getLocalHost()
will return the loopback address (in 127/8, usually 127.0.0.1). Thus the name taken from the /etc/hosts
file likely to be localhost.localdomain
.
Linux 上可能发生的情况是InetAddress.getLocalHost()
将返回环回地址(在 127/8 中,通常为 127.0.0.1)。因此,取自/etc/hosts
文件的名称可能是localhost.localdomain
.
In order to get the correct address / host name, you can use the following code instead, that will list all IP address associated with a network interface (eth0
in my example), and we'll select the IPv4 one, that does not belong to the loopback class.
为了获得正确的地址/主机名,您可以改用以下代码,该代码将列出与网络接口关联的所有 IP 地址(eth0
在我的示例中),我们将选择不属于的 IPv4 地址环回类。
try {
// Replace eth0 with your interface name
NetworkInterface i = NetworkInterface.getByName("eth0");
if (i != null) {
Enumeration<InetAddress> iplist = i.getInetAddresses();
InetAddress addr = null;
while (iplist.hasMoreElements()) {
InetAddress ad = iplist.nextElement();
byte bs[] = ad.getAddress();
if (bs.length == 4 && bs[0] != 127) {
addr = ad;
// You could also display the host name here, to
// see the whole list, and remove the break.
break;
}
}
if (addr != null) {
System.out.println( addr.getCanonicalHostName() );
}
} catch (...) { ... }
You could change a bit the code to display all addresses, see the comments inside the code.
您可以稍微更改代码以显示所有地址,请参阅代码中的注释。
edit
编辑
You might want also to iterate over other NICs, as suggested by @rafalmag
您可能还想遍历其他 NIC,如@rafalmag 所建议的
instead of NetworkInterface.getByName("eth0") I suggest to iterate over NetworkInterface.getNetworkInterfaces()
而不是 NetworkInterface.getByName("eth0") 我建议迭代 NetworkInterface.getNetworkInterfaces()