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) 状态
主机名可以是计算机名称,例如"java.sun",也可以是 IP地址的文本表示形式. 如果原义IP地址是 提供,则仅检查地址格式的有效性.
The host name can either be a machine name, such as "java.sun", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.
因此,它实际上并没有进入您的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
发布评论