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

StackOverflow https://stackoverflow.com/questions/22943036

  •  29-06-2023
  •  | 
  •  

Question

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?

Était-ce utile?

La 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.

Autres conseils

When you are connected to internet your laptop has the IP Addr 10.224.108.58; when you are not connected to internet, no IP addres is associated to the laptop so the default one is used; the default one is 127.0.0.1

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top