Localhost isn't localhost

Discussion in 'Java' started by EdwardH, Sep 29, 2005.

  1. EdwardH

    EdwardH Guest

    Why does...

    InetAddress inetAddress = InetAddress.getLocalHost();

    return 192.168.0.2? It should return 127.0.0.1, shouldn't it?
     
    EdwardH, Sep 29, 2005
    #1
    1. Advertising

  2. EdwardH

    Hal Rosser Guest

    "EdwardH" <edwardh@N:O:S:p:A:M:edward.dyndns.org> wrote in message
    news:wdH_e.147288$...
    > 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.
     
    Hal Rosser, Sep 29, 2005
    #2
    1. Advertising

  3. EdwardH

    Jeff Schwab Guest

    Hal Rosser wrote:
    > "EdwardH" <edwardh@N:O:S:p:A:M:edward.dyndns.org> wrote in message
    > news:wdH_e.147288$...
    >
    >>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.


    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.
     
    Jeff Schwab, Sep 29, 2005
    #3
  4. EdwardH wrote:
    > 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?
     
    puzzlecracker, Sep 29, 2005
    #4
  5. EdwardH

    Roedy Green Guest

    On Wed, 28 Sep 2005 22:20:24 -0400, Jeff Schwab
    <> wrote or quoted :

    >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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 29, 2005
    #5
  6. Jeff Schwab wrote:
    > Hal Rosser wrote:
    >
    >> "EdwardH" <edwardh@N:O:S:p:A:M:edward.dyndns.org> wrote in message
    >> news:wdH_e.147288$...
    >>
    >>> 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.

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

    --
    John Bollinger
     
    John C. Bollinger, Sep 29, 2005
    #6
  7. EdwardH

    E11 Guest

    Question:

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



    Regards,
    Edwin
     
    E11, Sep 29, 2005
    #7
  8. EdwardH

    Roedy Green Guest

    On 28 Sep 2005 22:01:20 -0700, "E11" <> wrote or
    quoted :

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


    see the code called WhoAmI listed at
    http://mindprod.com/jgloss/ip.html

    You can get them both.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 29, 2005
    #8
  9. wrote in comp.lang.java.programmer:
    > 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.

    --
    Antti S. Brax Rullalautailu pitää lapset poissa ladulta
    http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

    [1385 messages expunged from folder "Spam"]
     
    Antti S. Brax, Sep 29, 2005
    #9
  10. EdwardH

    Roedy Green Guest

    On Thu, 29 Sep 2005 01:14:04 GMT, EdwardH
    <edwardh@N:O:S:p:A:M:edward.dyndns.org> wrote or quoted :

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


    Your local host may have many aliases.


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 29, 2005
    #10
  11. EdwardH

    EdwardH Guest

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


    Sure. Doubt he'll be able to explain it.
     
    EdwardH, Sep 29, 2005
    #11
  12. EdwardH

    EdwardH Guest

    > 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.
     
    EdwardH, Sep 29, 2005
    #12
  13. EdwardH wrote:
    > 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."

    --
    Darryl L. Pierce <>
    Homepage: http://mcpierce.multiply.com/
    "Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant
     
    Darryl L. Pierce, Sep 29, 2005
    #13
  14. Jeff Schwab wrote:
    > 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.

    --
    Darryl L. Pierce <>
    Homepage: http://mcpierce.multiply.com/
    "Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant
     
    Darryl L. Pierce, Sep 29, 2005
    #14
  15. EdwardH wrote:
    >> 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.


    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.

    --
    Darryl L. Pierce <>
    Homepage: http://mcpierce.multiply.com/
    "Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant
     
    Darryl L. Pierce, Sep 29, 2005
    #15
  16. EdwardH

    Jeff Schwab Guest

    John C. Bollinger wrote:
    > Jeff Schwab wrote:
    >
    >> Hal Rosser wrote:
    >>
    >>> "EdwardH" <edwardh@N:O:S:p:A:M:edward.dyndns.org> wrote in message
    >>> news:wdH_e.147288$...
    >>>
    >>>> 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.

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


    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!
     
    Jeff Schwab, Sep 29, 2005
    #16
  17. EdwardH

    Roedy Green Guest

    On Thu, 29 Sep 2005 09:41:29 GMT, EdwardH
    <edwardh@N:O:S:p:A:M:edward.dyndns.org> wrote or quoted :

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


    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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 29, 2005
    #17
  18. EdwardH

    HalcyonWild Guest

    Roedy Green wrote:
    > 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.
     
    HalcyonWild, Sep 30, 2005
    #18
  19. EdwardH

    John English Guest

    HalcyonWild wrote:

    > 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:
    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)
    ----------------------------------------------------------------------
     
    John English, Sep 30, 2005
    #19
  20. HalcyonWild wrote:
    > 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.

    --
    Darryl L. Pierce <>
    Homepage: http://mcpierce.multiply.com/
    "Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant
     
    Darryl L. Pierce, Sep 30, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manfred Balik
    Replies:
    10
    Views:
    3,522
    Dave Pollum
    Jul 18, 2005
  2. Jiong Feng
    Replies:
    0
    Views:
    841
    Jiong Feng
    Nov 19, 2003
  3. Alex
    Replies:
    4
    Views:
    575
    Hasani Blackwell
    Feb 8, 2005
  4. =?Utf-8?B?S2VuTGVl?=
    Replies:
    5
    Views:
    2,717
  5. koicat
    Replies:
    0
    Views:
    228
    koicat
    Jul 2, 2010
Loading...

Share This Page