0
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?

ifloop
  • 8,079
  • 2
  • 26
  • 35
Samitha Chathuranga
  • 1,689
  • 5
  • 30
  • 57

2 Answers2

1

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.

nimsson
  • 930
  • 1
  • 14
  • 27
  • So when I am connected to internet there should be 2 localhost IP addresses as u say? So then I should be able to get both these IP addresses as localhost when connected to internet. Is that so.? If yes how to get that? – Samitha Chathuranga Apr 08 '14 at 17:24
  • It chooses one of them. To get all of them see http://stackoverflow.com/questions/494465/how-to-enumerate-ip-addresses-of-all-enabled-nic-cards-from-java – nimsson Apr 08 '14 at 17:51
1

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

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65