Finding an IP address

Discussion in 'Java' started by Ike, Sep 30, 2004.

  1. Ike

    Ike Guest

    If I have a client, plugged directly into the internet, say, on a phone
    modem, I can get the IP with:

    InetAddress.getLocalHost().getHostAddress();

    However, if I am also plugged into an ethernet, AND directly into the
    internet(again, say, with a phone line) that same call,
    InetAddress.getLocalHost().getHostAddress(),
    only gets me the IP on the LAN. How can I, under such a circumstance, find
    out what the IP is of a given internet connection in such a case? Thanks,
    Ike
     
    Ike, Sep 30, 2004
    #1
    1. Advertising

  2. Ike

    Paul Lutus Guest

    Ike wrote:

    > If I have a client, plugged directly into the internet, say, on a phone
    > modem, I can get the IP with:
    >
    > InetAddress.getLocalHost().getHostAddress();
    >
    > However, if I am also plugged into an ethernet, AND directly into the
    > internet(again, say, with a phone line) that same call,
    > InetAddress.getLocalHost().getHostAddress(),
    > only gets me the IP on the LAN. How can I, under such a circumstance, find
    > out what the IP is of a given internet connection in such a case?


    "Of a given Internet connection"? Does this mean a specific NIC? You really
    cannot reliably do this from Java.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 30, 2004
    #2
    1. Advertising

  3. Ike

    John Davison Guest

    Paul Lutus wrote:

    > Ike wrote:
    >
    >
    >>If I have a client, plugged directly into the internet, say, on a phone
    >>modem, I can get the IP with:
    >>
    >> InetAddress.getLocalHost().getHostAddress();
    >>
    >>However, if I am also plugged into an ethernet, AND directly into the
    >>internet(again, say, with a phone line) that same call,
    >>InetAddress.getLocalHost().getHostAddress(),
    >>only gets me the IP on the LAN. How can I, under such a circumstance, find
    >>out what the IP is of a given internet connection in such a case?

    >
    >
    > "Of a given Internet connection"? Does this mean a specific NIC? You really
    > cannot reliably do this from Java.
    >


    Actually you can since 1.4

    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
    NetworkInterface iface = (NetworkInterface)e.nextElement();
    System.out.println(iface.getName());
    }

    You can search by name I guess.

    - john
     
    John Davison, Sep 30, 2004
    #3
  4. "Ike" <> writes:

    > How can I, under such a circumstance, find out what the IP is of a
    > given internet connection in such a case? Thanks, Ike


    You need to obtain a Socket going to a remote internet address, and
    call getLocalAddress().
     
    Tor Iver Wilhelmsen, Sep 30, 2004
    #4
  5. Ike wrote:
    > If I have a client, plugged directly into the internet, say, on a phone
    > modem, I can get the IP with:
    >
    > InetAddress.getLocalHost().getHostAddress();
    >
    > However, if I am also plugged into an ethernet, AND directly into the
    > internet(again, say, with a phone line) that same call,
    > InetAddress.getLocalHost().getHostAddress(),
    > only gets me the IP on the LAN. How can I, under such a circumstance, find
    > out what the IP is of a given internet connection in such a case? Thanks,
    > Ike
    >
    >

    Well, unless you are behind a box that's doing Network Address Translation,
    the LAN IP address will be the address seen on the internet. If you are
    going through a NAT box, you have no option but to ask the machine you
    are connected to what IP address you APPEAR to have to him.

    Steve
     
    Steve Horsley, Sep 30, 2004
    #5
  6. Steve Horsley wrote:
    > Well, unless you are behind a box that's doing Network Address Translation,
    > the LAN IP address will be the address seen on the internet.


    Not if you're connected to the internet via a different interface, e.g. a modem.
     
    Michael Borgwardt, Sep 30, 2004
    #6
  7. Michael Borgwardt wrote:
    > Steve Horsley wrote:
    >
    >> Well, unless you are behind a box that's doing Network Address
    >> Translation,
    >> the LAN IP address will be the address seen on the internet.

    >
    >
    > Not if you're connected to the internet via a different interface, e.g.
    > a modem.


    Urgh. I completely misread the question!
    Seems I speed-read and missed a line and a half.

    InetAddress.getAllByName(hostname) will retrieve the addresses, but
    I suppose you have to guess which is your internet interface's address.

    Steve
     
    Steve Horsley, Sep 30, 2004
    #7
  8. Ike

    Alex Hunsley Guest

    Ike wrote:
    > If I have a client, plugged directly into the internet, say, on a phone
    > modem, I can get the IP with:
    >
    > InetAddress.getLocalHost().getHostAddress();
    >
    > However, if I am also plugged into an ethernet, AND directly into the
    > internet(again, say, with a phone line) that same call,
    > InetAddress.getLocalHost().getHostAddress(),
    > only gets me the IP on the LAN. How can I, under such a circumstance, find
    > out what the IP is of a given internet connection in such a case? Thanks,
    > Ike


    The other replies have suggested a few network-techy answers which may
    do what you want.
    If that fails, one possibility is to have your java code fetch the page:

    http://www.whatismyip.com/

    ....and then parse out the IP displayed from the HTML you get back.
    This has the advantage that you will definitely be seeing the IP address
    that this website thinks you are at, which will be your external
    internet IP, and not a local one by mistake.

    alex
     
    Alex Hunsley, Oct 1, 2004
    #8
    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. Replies:
    1
    Views:
    624
    Alex Molochnikov
    Nov 18, 2005
  2. perspolis

    finding address in browser?

    perspolis, May 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 3, 2006
  3. Replies:
    3
    Views:
    376
    Ron Natalie
    Apr 30, 2005
  4. Chris

    finding IP address of computer

    Chris, Apr 27, 2006, in forum: Python
    Replies:
    9
    Views:
    439
    Paul Watson
    May 1, 2006
  5. =?Utf-8?B?T3dlbiBSaWNoYXJkc29u?=

    Finding Email address in a Profile

    =?Utf-8?B?T3dlbiBSaWNoYXJkc29u?=, Aug 24, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    394
    Ken Cox [Microsoft MVP]
    Aug 24, 2006
Loading...

Share This Page