Samitha Chathuranga
Samitha Chathuranga

Reputation: 1749

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?

Upvotes: 0

Views: 558

Answers (2)

Angelo Immediata
Angelo Immediata

Reputation: 6954

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

Upvotes: 1

nimsson
nimsson

Reputation: 950

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.

Upvotes: 1

Related Questions