Finding Internet IP instead of network IP

Discussion in 'Java' started by zach.bastick@gmail.com, Feb 21, 2005.

  1. Guest

    Hello

    I am writing an application that needs to use the actual IP of a local
    machine. I have tried multiple methods of finding the IP, but it seems
    that each method simply returns an IP of a network interface, which is
    not always the internet ip of the computer.

    InetAddress in = InetAddress.getLocalHost();
    InetAddress[] all = InetAddress.getAllByName(in.getHostName());
    for (int i=0; i<all.length; i++) {
    System.out.println(" address = " + all);
    }

    The output of the following code on my machine is:

    address = blue-monster/192.168.0.244
    address = blue-monster/10.0.3.71

    ...None of these are internet addresses. How do I find my internet IP?

    Thanks a lot

    Zach


    P.S. info about my connection:
    I am connected to the internet through a local network. Another
    computer on the network (specifically 10.0.3.251) is my gateway. I can
    get an internet ip by viewing whatismyip.com.
     
    , Feb 21, 2005
    #1
    1. Advertising

  2. Oscar kind Guest

    wrote:
    > I am writing an application that needs to use the actual IP of a local
    > machine. I have tried multiple methods of finding the IP, but it seems
    > that each method simply returns an IP of a network interface, which is
    > not always the internet ip of the computer.
    >
    > InetAddress in = InetAddress.getLocalHost();
    > InetAddress[] all = InetAddress.getAllByName(in.getHostName());
    > for (int i=0; i<all.length; i++) {
    > System.out.println(" address = " + all);
    > }
    >
    > The output of the following code on my machine is:
    >
    > address = blue-monster/192.168.0.244
    > address = blue-monster/10.0.3.71
    >
    > ..None of these are internet addresses. How do I find my internet IP?


    You did. Your computer doesn't know about the internet. It just knows that
    if it want to send a packet to another computer on the same network, it
    can do it itself. It also knows that if it want to send a packet
    elsewhere, it uses a gateway (your router most likely).

    So what you want to do is get all addresses of your router, and discard
    the loopback address and the address on your network. If you have only one
    network, you'll now have one address left: the address you're known with
    on the internet.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Feb 21, 2005
    #2
    1. Advertising

  3. Juhan Kundla Guest

    wrote:

    [...]

    > The output of the following code on my machine is:
    >
    > address = blue-monster/192.168.0.244
    > address = blue-monster/10.0.3.71
    >
    > ..None of these are internet addresses. How do I find my internet IP?


    You can't, because "your" Internet address is not really yours. It is
    the IP-address assigned to the router network interface, not to your
    computer. The router does silently a thing called native address
    translation and from your computer point of view, you are not supposed
    to know anything about it.

    You could either write a program, which goes to router and ask its
    public IP address, or you could send a IP-packet to an other machine in
    Internet and later ask, what this machine thinks the source address was.
    The latter is exactly the case with whatismyip.com. Perhaps you could
    write a simple java HTTP-client, which goes to the whatismyip.com and
    parses the HTML-page.


    Juhan
     
    Juhan Kundla, Feb 22, 2005
    #3
    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. Dave
    Replies:
    0
    Views:
    660
  2. cmay
    Replies:
    0
    Views:
    1,180
  3. hood
    Replies:
    1
    Views:
    785
    Andrew Thompson
    Dec 30, 2003
  4. Elliot
    Replies:
    3
    Views:
    375
    Elliot
    Jun 16, 2008
  5. Shailesh Patel
    Replies:
    0
    Views:
    491
    Shailesh Patel
    Nov 8, 2006
Loading...

Share This Page