Localhost isn't localhost

E

EdwardH

Why does...

InetAddress inetAddress = InetAddress.getLocalHost();

return 192.168.0.2? It should return 127.0.0.1, shouldn't it?
 
H

Hal Rosser

EdwardH said:
Why does...

InetAddress inetAddress = InetAddress.getLocalHost();

return 192.168.0.2? It should return 127.0.0.1, shouldn't it?

The true IP address is returned.
127.0.0.1 ius just the ring-back address for 'any' machine.
It would be useless to get 127.0.0.1 every time. That could be hard-coded.
 
J

Jeff Schwab

Hal said:
The true IP address is returned.
127.0.0.1 ius just the ring-back address for 'any' machine.
It would be useless to get 127.0.0.1 every time. That could be hard-coded.

Probably not the "true" IP address. That looks like a NAT address,
probably from a machine connected to a small router. The router is
probably at 192.168.0.1.
 
P

puzzlecracker

EdwardH said:
Why does...

InetAddress inetAddress = InetAddress.getLocalHost();

return 192.168.0.2? It should return 127.0.0.1, shouldn't it?

I dunno..I will ask my professor tomorrow as the first thing in the
morning. can you wait?
 
R

Roedy Green

Probably not the "true" IP address. That looks like a NAT address,
probably from a machine connected to a small router. The router is
probably at 192.168.0.1.

192.168.0.2 would the IP address of that machine on the LAN, the one
it responds to when the router forwards stuff to it.. To the outside
world, all the machines on the LAN look like the same machine coming
from the "face IP" -- the external IP of the router. To find the face
IP see http://mindprod.com/jgloss/ip.html
 
J

John C. Bollinger

Jeff said:
Probably not the "true" IP address. That looks like a NAT address,
probably from a machine connected to a small router. The router is
probably at 192.168.0.1.

You choose an odd definition of "true". The address is one that is
configured on a local network interface; from the machine's own point of
view you can't get any more true than that. You are correct that the
address is non-routable, which may (or may not) signal that the machine
is behind a NAT service, but that is outside the scope of the inquiry.
It important to recognize that the address obtained this way is not
guaranteed to be useful in any particular broader scope, yes, but there
are many reasons why that might be true of any network address.
 
E

E11

Question:

If i have two Ethernet cards installed, (i.e. 2 interfaces), what would
InetAddress.getLocalHost()
return?



Regards,
Edwin
 
A

Antti S. Brax

Question:

If i have two Ethernet cards installed, (i.e. 2 interfaces), what would
InetAddress.getLocalHost()
return?

Read the API: the IP address of the local host.

The IP address of localhost is 127.0.0.1.

Note that localhost is not bound to a physical network card. It
exists even if you remove all ethernet cards.
 
E

EdwardH

Your local host may have many aliases.

Except "localhost" is universally known to resolve to 127.0.0.1 ...
except in Java, for some reason.
 
D

Darryl L. Pierce

EdwardH said:
Why does...

InetAddress inetAddress = InetAddress.getLocalHost();

return 192.168.0.2? It should return 127.0.0.1, shouldn't it?

If it were going to return a constant value like the lo interface, then
it would be a constant and not a method. getLocalHost() returns the true
address for the machine. Otherwise, "if the operation is not allowed, an
InetAddress representing the loopback address is returned."
 
D

Darryl L. Pierce

Jeff said:
Probably not the "true" IP address. That looks like a NAT address,
probably from a machine connected to a small router. The router is
probably at 192.168.0.1.

If it's assigned to the machine, then it's a true address. It may not be
the *public* address (i.e., what's visible from the 'net itself) but
it's a true address.
 
D

Darryl L. Pierce

EdwardH said:
Except "localhost" is universally known to resolve to 127.0.0.1 ...
except in Java, for some reason.

But, you didn't use the API InetAddress.getByName("localhost"), which
would resolve to 127.0.0.1. You used InetAddress.getLocalHost() which
returns the IP address for your machine. There's a very clear difference
there.
 
J

Jeff Schwab

John said:
You choose an odd definition of "true".

Routable from outside the LAN.
The address is one that is
configured on a local network interface; from the machine's own point of
view you can't get any more true than that. You are correct that the
address is non-routable, which may (or may not) signal that the machine
is behind a NAT service, but that is outside the scope of the inquiry.
It important to recognize that the address obtained this way is not
guaranteed to be useful in any particular broader scope, yes, but there
are many reasons why that might be true of any network address.

Fair enough!
 
R

Roedy Green

Except "localhost" is universally known to resolve to 127.0.0.1 ...
except in Java, for some reason

localhost does resolve to 127.0.0.1. see
http://mindprod.com/jgloss/ip.html
and look at the code for WhoIs.

However, getLocalHost gives you the actual IP on the LAN of the local
host. Perhaps that method is misnamed. There is no need for a method
that always returns 127.0.0.1

There is no method to get you the face ip. That requires co-operation
from some outside server.

This is admittedly a lawyerly response.
 
H

HalcyonWild

Roedy said:
192.168.0.2 would the IP address of that machine on the LAN, the one
it responds to when the router forwards stuff to it.. To the outside
world, all the machines on the LAN look like the same machine coming
from the "face IP" -- the external IP of the router. To find the face
IP see http://mindprod.com/jgloss/ip.html

On the windows command prompt, when I type ping localhost it gives me
127.0.0.1, when I say ping myMachineName.domain.com it gives me the IP
in the 192.168 series.
Is there anything special about 192.168 series. I saw the mindprod web
page mentioned, but did not find anything mentioned in particular,
except, for use in private Lans.
Is it true, that,
1. to my machine, my machine is 127.0.0.1
2. to other machines in my LAN, my machine is 192.168.x.x
3. to machines in the outside world of my LAN, my machine is something
else in the public domain.
 
J

John English

HalcyonWild said:
Is there anything special about 192.168 series. I saw the mindprod web
page mentioned, but did not find anything mentioned in particular,

The short answer is yes. For the long answer, ask Google to find you a
copy of RFC1597 & RFC1918.

----------------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | "Those who don't know their history
University of Brighton | are condemned to relive it" (Santayana)
----------------------------------------------------------------------
 
D

Darryl L. Pierce

HalcyonWild said:
Is there anything special about 192.168 series.

Check out this page: http://www.faqs.org/rfcs/rfc1918.html

Your router is also your DHCP server, and it's handing out non-routable
IP addresses; i.e., they will not be broadcast over the Internet. The
router is then acting as a NAT and translating those addresses into
something that can be broadcast over the 'net. When the response is
received, the router then translates that response and sends it to the
right machine on the private network.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top