Search code examples
javaiphostinetaddress

InetAddress getHostAddress() method gives different outputs when the laptop is connected to internet and not connected to internet?


InetAddress address= InetAddress.getLocalHost();

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

There are 2 types of Outputs for above codes at 2 different occations.

They are:

1) when internet is connected: Samitha-Pc------10.224.108.58
2) when internet is not connected): Samitha-Pc------127.0.0.1

What is the reason for this difference? How the host has been changed from localhost to the ISP given ip?


Solution

  • 127.0.0.1 is the internal ip (localhost). When you are connected to the router, it assigns you another ip address. 10.224.108.58 is the ip assigned by your router. See http://www.howtogeek.com/126304/why-is-the-localhost-ip-127.0.0.1/. When you are not connected to the internet, the only available ip is localhost.