本文介绍了“本地主机"与127.0.0.1 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:35
查看更多