如何获得完整的主机名?

时间:2015-02-24 16:41:41

标签: java networking network-programming hostname

当我在cmd中编写以下代码时:

ping localhost

我看到以下结果:

Pinging hostname.A.B.com [::1] with 32 bytes of data:
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms

写作时

InetAddress.getLocalHost().getCanonicalHostName();

仅返回hostname

如何从java代码返回hostname.A.B.com

P.S。

不重复。我使用顶部答案的代码,但我没有达到预期的结果。

1 个答案:

答案 0 :(得分:0)

在您的情况下,您获取 localhost 地址InetAddress.getLocalHost(),因此如果您没有为主机设置完全限定名称,则只能获取主机名。在linux(可能还有OS X和其他UNIX系统)中,您需要将您的限定主机名添加到/etc/hosts到您的localhost条目才能生效。

相关问题