在Java中获取主机名的推荐方法 [英] Recommended way to get hostname in Java

查看:388
本文介绍了在Java中获取主机名的推荐方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下哪项是以Java最佳和最便捷的方式获取当前计算机的主机名?

Which of the following is the best and most portable way to get the hostname of the current computer in Java?

Runtime.getRuntime( ).exec(hostname)

vs

InetAddress.getLocalHost()。getHostName()

推荐答案

严格来说 - 你没有选择但是调用 hostname(1)或 - 在Unix gethostname(2)上。这是你电脑的名字。任何尝试通过这样的IP地址确定主机名

Strictly speaking - you have no choice but calling either hostname(1) or - on Unix gethostname(2). This is the name of your computer. Any attempt to determine the hostname by an IP address like this

InetAddress.getLocalHost().getHostName()

在某些情况下必然会失败:

is bound to fail in some circumstances:


  • IP地址可能无法解析为任何名称。恶意的DNS设置,糟糕的系统设置或错误的提供程序设置可能是其原因。

  • DNS中的名称可以包含许多名为CNAME的别名。这些只能在一个方向正确解决:姓名地址。相反的方向是不明确的。哪一个是官方名称?

  • 主机可以有多个不同的IP地址,每个地址可以有不同的名称。两个常见的情况是:一个以太网端口有几个逻辑IP地址,或者计算机有几个以太网端口。它是可配置的,无论它们是共享一个IP还是具有不同的IP。这被称为多宿主。

  • DNS中的一个名称可以解析为多个IP地址。并不是所有这些地址都必须位于同一台计算机上! (Usecase:一种简单的负载均衡形式)

  • 我们甚至不会开始谈论动态IP地址。

  • The IP address might not resolve into any name. Bad DNS setup, bad system setup or bad provider setup may be the reason for this.
  • A name in DNS can have many aliases called CNAMEs. These can only be resolved in one direction properly: name to address. The reverse direction is ambiguous. Which one is the "official" name?
  • A host can have many different IP addresses - and each address can have many different names. Two common cases are: One ethernet port has several "logical" IP addresses or the computer has several ethernet ports. It is configurable whether they share an IP or have different IPs. This is called "multihomed".
  • One Name in DNS can resolve to several IP Addresses. And not all of those addresses must be located on the same computer! (Usecase: A simple form of load-balancing)
  • Let's not even start talking about dynamic IP addresses.

也不要将IP地址的名称与主机名称(主机名)混淆。隐喻可能会更清楚:

Also don't confuse the name of an IP-address with the name of the host (hostname). A metaphor might make it clearer:


有一个叫做伦敦的大城市(服务器)。在城墙内,很多事情发生。城市有几个门(IP地址)。每个门都有一个名字(北门,河门,南安普敦门...),但门的名字不是城市的名称。另外你也不能用一个门的名字来推断城市的名字 - 北门会吸引一半的大城市,而不仅仅是一个城市。然而,一个陌生人(IP包)沿着河流走去,问一个地方:我有一个奇怪的地址:Rivergate,左二,第三宫,你能帮我吗?当地人说:当然,你在正确的道路上,只需要在半小时内到达目的地。

There is a large city (server) called "London". Inside the city walls much business happens. The city has several gates (IP addresses). Each gate has a name ("North Gate", "River Gate", "Southampton Gate"...) but the name of the gate is not the name of the city. Also you cannot deduce the name of the city by using the name of a gate - "North Gate" would catch half of the bigger cities and not just one city. However - a stranger (IP packet) walks along the river and asks a local: "I have a strange address: 'Rivergate, second left, third house'. Can you help me?" The local says: "Of course, you are on the right road, simply go ahead and you will arrive at your destination within half an hour."

这个例子说明了我的想法。

This illustrates it pretty much I think.

好消息是:真正的主机名通常是 >没有必要。在大多数情况下,任何解析为该主机IP地址的名称都可以。 (陌生人可能由Northgate进入城市,但有帮助的当地人翻译第二左部分。)

The good news is: The real hostname is usually not necessary. In most cases any name which resolves into an IP address on this host will do. (The stranger might enter the city by Northgate, but helpful locals translate the "2nd left" part.)

如果剩下的角落必须使用 此配置设置的源代码,即C函数 gethostname(2)。该函数也由程序 hostname 调用。

If the remaining corner cases you must use the definitive source of this configuration setting - which is the C function gethostname(2). That function is also called by the program hostname.

这篇关于在Java中获取主机名的推荐方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆