“本地主机"与127.0.0.1 Java

编程入门 行业动态 更新时间:2024-10-09 04:21:13
本文介绍了“本地主机"与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) 状态

主机名可以是计算机名称,例如"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

本文发布于:2023-11-26 21:36:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主机   quot   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!