Getting A Computers IP Address

Discussion in 'Java' started by Hal Vaughan, Dec 8, 2007.

  1. Hal Vaughan

    Hal Vaughan Guest

    I know there are different ways to get a computers IP address. I'd like to
    have a Java program find out a computer's IP address for the Internet, not
    for any LAN it's on. Is there a way I can do this easily or by finding the
    gateway's or proxy's eternal IP address?

    Thanks!

    Hal
     
    Hal Vaughan, Dec 8, 2007
    #1
    1. Advertising

  2. Hal Vaughan wrote:
    > I know there are different ways to get a computers IP address. I'd like to
    > have a Java program find out a computer's IP address for the Internet, not
    > for any LAN it's on. Is there a way I can do this easily or by finding the
    > gateway's or proxy's eternal IP address?


    As far as I know there is no general way of doing this. It is one of the
    issues addressed by UPnP. If your gateway supports UPnP that would
    provide a mechanism, and a Java implementation is possible. Some people
    think UPnP is a security risk and disable it, so unless you have
    management authority over the LAN you may not be able to use this
    approach even if the hardware supports it.

    Mark Thornton
     
    Mark Thornton, Dec 8, 2007
    #2
    1. Advertising

  3. Hal Vaughan

    Arne Vajhøj Guest

    Hal Vaughan wrote:
    > I know there are different ways to get a computers IP address. I'd like to
    > have a Java program find out a computer's IP address for the Internet, not
    > for any LAN it's on. Is there a way I can do this easily or by finding the
    > gateway's or proxy's eternal IP address?


    There are not even a 100% safe way.

    You will need to ask an external server what IP it see.

    Below are a small example using a danish service.

    Arne

    ============================

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class ExternIP {
    public static String getIP() throws Exception {
    URLConnection uc = (new
    URL("http://www.myip.dk/")).openConnection();
    BufferedReader br = new BufferedReader(new
    InputStreamReader(uc.getInputStream()));
    StringBuilder sb = new StringBuilder("");
    String line;
    while((line = br.readLine()) != null) {
    sb.append(line);
    }
    br.close();
    Pattern p = Pattern.compile("(?:<title>Your IP:
    )(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:</title>)");
    Matcher m = p.matcher(sb.toString());
    m.find();
    return m.group(1);
    }
    public static void main(String[] args) throws Exception {
    System.out.println(ExternIP.getIP());
    }
    }
     
    Arne Vajhøj, Dec 8, 2007
    #3
  4. Arne Vajhøj wrote:
    > Hal Vaughan wrote:
    >> I know there are different ways to get a computers IP address. I'd
    >> like to
    >> have a Java program find out a computer's IP address for the Internet,
    >> not
    >> for any LAN it's on. Is there a way I can do this easily or by
    >> finding the
    >> gateway's or proxy's eternal IP address?

    >
    > There are not even a 100% safe way.
    >
    > You will need to ask an external server what IP it see.
    >
    > Below are a small example using a danish service.
    >


    So called "transparent" proxies often mean such attempts return the
    address of the proxy instead of your networks external address.

    Mark Thornton
     
    Mark Thornton, Dec 8, 2007
    #4
  5. Hal Vaughan

    Arne Vajhøj Guest

    Mark Thornton wrote:
    > Arne Vajhøj wrote:
    >> Hal Vaughan wrote:
    >>> I know there are different ways to get a computers IP address. I'd
    >>> like to
    >>> have a Java program find out a computer's IP address for the
    >>> Internet, not
    >>> for any LAN it's on. Is there a way I can do this easily or by
    >>> finding the
    >>> gateway's or proxy's eternal IP address?

    >>
    >> There are not even a 100% safe way.
    >>
    >> You will need to ask an external server what IP it see.
    >>
    >> Below are a small example using a danish service.
    >>

    >
    > So called "transparent" proxies often mean such attempts return the
    > address of the proxy instead of your networks external address.


    Assuming that it will be the same proxy for such a site and the
    real site then it does not matter.

    Arne
     
    Arne Vajhøj, Dec 8, 2007
    #5
  6. Hal Vaughan wrote:

    Re: Getting A Computers IP Address

    Why? What does that do for the end user?*

    Had you considered ..

    <sscce>
    import javax.swing.*;

    class GetIPAddress {

    public static void main(String[] args) {
    JOptionPane.showMessageDialog(
    null,
    "The appl. needs you IP address so it can AAA\n" +
    "To find the IP address on your system, do BBB");
    String ipAddress = JOptionPane.showInputDialog(
    null,
    "IP address");
    System.out.println("IP Address: " + ipAddress);
    }
    }
    </sscce>

    Where AAA is a text that is sufficiently convincing that
    it would *motivate me, as an end user, to supply an IP
    address, and BBB is OS specific instructions.

    Easy done!

    --
    Andrew Thompson
    http://www.physci.org/

    Message posted via http://www.javakb.com
     
    Andrew Thompson, Dec 9, 2007
    #6
  7. Arne Vajhøj wrote:
    > Mark Thornton wrote:
    >> Arne Vajhøj wrote:
    >>> Hal Vaughan wrote:
    >>>> I know there are different ways to get a computers IP address. I'd
    >>>> like to
    >>>> have a Java program find out a computer's IP address for the
    >>>> Internet, not
    >>>> for any LAN it's on. Is there a way I can do this easily or by
    >>>> finding the
    >>>> gateway's or proxy's eternal IP address?
    >>>
    >>> There are not even a 100% safe way.
    >>>
    >>> You will need to ask an external server what IP it see.
    >>>
    >>> Below are a small example using a danish service.
    >>>

    >>
    >> So called "transparent" proxies often mean such attempts return the
    >> address of the proxy instead of your networks external address.

    >
    > Assuming that it will be the same proxy for such a site and the
    > real site then it does not matter.
    >
    > Arne


    The proxy depends on the port, so you have to do the test using the same
    port as your later intended use of the IP address. My ISP has
    transparent proxies on port 80.

    Mark Thornton
     
    Mark Thornton, Dec 9, 2007
    #7
  8. Hal Vaughan

    Mark Rafn Guest

    Hal Vaughan <> wrote:
    >I know there are different ways to get a computers IP address. I'd like to
    >have a Java program find out a computer's IP address for the Internet, not
    >for any LAN it's on. Is there a way I can do this easily or by finding the
    >gateway's or proxy's eternal IP address?


    Have the server report the address of the endpoint of the connection. This
    will give you the NAT or proxy server address.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Dec 9, 2007
    #8
  9. Hal Vaughan

    Roedy Green Guest

    On Sat, 08 Dec 2007 15:46:18 -0500, Hal Vaughan
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I know there are different ways to get a computers IP address. I'd like to
    >have a Java program find out a computer's IP address for the Internet, not
    >for any LAN it's on. Is there a way I can do this easily or by finding the
    >gateway's or proxy's eternal IP address?


    Usually internal IPs are masked by the firewall or proxy server. For
    the various IPs you can discover, see
    http://mindprod.com/jgloss/ip.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Dec 10, 2007
    #9
    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. WWallace
    Replies:
    0
    Views:
    695
    WWallace
    Oct 23, 2003
  2. Weng Tianxiang
    Replies:
    0
    Views:
    517
    Weng Tianxiang
    Apr 8, 2005
  3. Alexander Duerloo

    Client computers IP-address?

    Alexander Duerloo, Feb 8, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,769
    DalePres
    Feb 8, 2004
  4. Chris
    Replies:
    1
    Views:
    338
    Veronica Jacobs
    Jun 1, 2004
  5. Andy B
    Replies:
    4
    Views:
    1,236
    Alexey Smirnov
    Dec 19, 2008
Loading...

Share This Page