0
InetAddress address= InetAddress.getLocalHost();

System.out.println(address.getHostName()+ "------"+address.getHostAddress());

上述代码在 2 个不同的场合有 2 种类型的输出。

他们是:

1)连接互联网时: Samitha-Pc------10.224.108.58
2)未连接互联网时): Samitha-Pc------127.0.0.1

造成这种差异的原因是什么?主机如何从 localhost 更改为给定 ip 的 ISP?

4

2 回答 2

1

127.0.0.1是内部 ip ( localhost)。当您连接到路由器时,它会为您分配另一个 IP 地址。10.224.108.58是你的路由器分配的ip。请参阅http://www.howtogeek.com/126304/why-is-the-localhost-ip-127.0.0.1/。当您未连接到 Internet 时,唯一可用的 ip 是 localhost。

于 2014-04-08T16:59:42.063 回答
1

当您连接到互联网时,您的笔记本电脑的 IP 地址为 10.224.108.58;当您没有连接到互联网时,没有 IP 地址与笔记本电脑关联,因此使用默认地址;默认值为 127.0.0.1

于 2014-04-08T17:00:50.740 回答