【问题标题】:InetAddress getHostAddress() method gives different outputs when the laptop is connected to internet and not connected to internet?当笔记本电脑连接到互联网而不连接到互联网时,InetAddress getHostAddress() 方法会给出不同的输出?
【发布时间】:2014-04-08 16:33:57
【问题描述】:
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?

【问题讨论】:

    标签: java ip host inetaddress


    【解决方案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/。当您没有连接到互联网时,唯一可用的 ip 是 localhost。

    【讨论】:

    • 所以当我连接到互联网时,应该有 2 个本地主机 IP 地址,如你所说?因此,当连接到 Internet 时,我应该能够将这两个 IP 地址作为 localhost 获取。是这样吗。?如果是,如何获得?
    • 它选择其中之一。要获得所有这些,请参阅stackoverflow.com/questions/494465/…
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多