问题描述
Java为 InetAddress.getByName("localhost").getHostAddress()提供了IP 127.0.0.1.但是,为什么Java不会为InetAddress.getByName("127.0.0.1").getHostName赋予"localhost".对于以后的版本,我得到"127.0.0.1"作为主机名.请澄清这一点.
Java is giving 127.0.0.1 as IP for InetAddress.getByName("localhost").getHostAddress()But why java not gives "localhost" for InetAddress.getByName("127.0.0.1").getHostName. For later one I get "127.0.0.1" as host name. Please clarify this.
推荐答案
InetAddress.getByName(String)
状态
因此,它实际上并没有进入您的hosts
文件(或DNS)以获取IP地址.它只是使用从您提供的String
创建的主机名和地址创建一个InetAddress
对象.
So it doesn't actually go to your hosts
file (or DNS) for an IP address. It just creates a InetAddress
object with both hostname and address created from the String
you provided.
第一个例子
InetAddress.getByName("localhost").getHostAddress()
假设您有一个hosts
文件条目,例如
Assuming you have a hosts
file entry like
127.0.0.1 localhost
然后返回的InetAddress
对象将具有该信息,即.主机名localhost
和地址127.0.0.1
.
then the InetAddress
object returned will have that information, ie. a hostname of localhost
and an address of 127.0.0.1
.
如果有的话,类似地
1.2.3.4 this.is.a.name
和
InetAddress localhost = InetAddress.getByName("this.is.a.name");
返回的InetAddress
将使用主机名this.is.a.name
和地址1.2.3.4
构造,因为它实际上已经过检查.
The returned InetAddress
would be constructed with a hostname of this.is.a.name
and an address of 1.2.3.4
, because it actually went and checked.
这篇关于“本地主机"与127.0.0.1 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!