网络编程“localhost”vs 127.0.0.1 java
Java将127.0.0.1作为InetAddress的IP地址。getByName(“本地主机”)。getHostAddress() 但是为什么java没有为InetAddress提供“localhost”。getByName(“127.0.0.1”)。获取主机名。对于后面的一个,我将“127.0.0.1”作为主机名。请澄清这一点
你可以在下面搜索框中键入要查询的问题!
Java将127.0.0.1作为InetAddress的IP地址。getByName(“本地主机”)。getHostAddress() 但是为什么java没有为InetAddress提供“localhost”。getByName(“127.0.0.1”)。获取主机名。对于后面的一个,我将“127.0.0.1”作为主机名。请澄清这一点
# 1 楼答案
^{} 州的javadoc
所以它实际上并没有进入你的
hosts
文件(或DNS)来获取IP地址。它只是创建一个InetAddress
对象,其中包含从您提供的String
创建的主机名和地址第一个例子
假设你有一个
hosts
文件条目,比如然后返回的
InetAddress
对象将包含该信息,即localhost
的主机名和127.0.0.1
的地址同样,如果你有
及
返回的
InetAddress
将由主机名this.is.a.name
和地址1.2.3.4
构成,因为它实际上是去检查的