Tech Off Thread

11 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

detecting public ip

Back to Forum: Tech Off
  • User profile image

    i writing a program to which i need the machine's public ip address. any one have a suggestion on how to get that ip?

    the only solution i can think of is accessing a site like and extracting the ip from the html. :/

  • User profile image

    You could walk through the interfaces, but that wouldn't help if you were behind a NAT router.

    It's a lot easier to detect the connecting IP from the other end of the connection, which is why things like exist.

  • User profile image

    You could then make a request to and parse the response. Low-tech, but it works.

  • User profile image

    Get an app like directupdate.  Works great with a crap-load of options for IP detection.

  • User profile image

    The December MSDN magazine has an article about this. Here is a link 

  • User profile image
    Sven Groot

    You can use this webservice to get public IP information, just dump the URL in the Add Web Reference dialog in VS and you're good to go.

    The web service belongs to this MSDN Magazine article which also shows an example on how to use it.

  • User profile image

    good article Smiley

  • User profile image

    It would be wise to run your own webservice. You never know if 3rd party service is running or if the url changes.

  • User profile image

    Do NOT do this. Expressionless

    I had this silly idea some time ago and it's too stupid for not sharing [A]

    One could once a month or so download the servers.ini from ****.com or one of its mirrors, then go through the servers.ini, (identd should be running and the host not firewalled plus a bunch of other stuff) and on successful connect query your own ip.

  • User profile image

    sorry i failed to mention that the project was in java therefore the specifics didn't apply but it is the same principle anyway Smiley

    so this is what i ended up doing...

    1) i created a page that detects the visitors ip using some simple PHP:
    <?php $ip = $_SERVER['REMOTE_ADDR']; ?>
    <title><?php echo $ip; ?></title>
    <?php echo "Your IP is: " . $ip; ?>

    2) i created a method to fetch the ip from the page
    //Gets the public IP of the current machine
        public static String FetchPublicIP() {
            String ip = "";
            try {
                URL youRL = new URL(""); //default service
                BufferedReader in = new BufferedReader(new InputStreamReader(youRL.openStream()));
                String s = "";
                while ((s = in.readLine()) != null) {
                    if (s.startsWith("<title>")) {
                        ip = s.substring(s.indexOf('>')+1, s.lastIndexOf('<'));
            } catch (IOException e) {}
            if (isValidHost(ip)) //make sure ip found is valid
                return ip;
                return "";

    thanks, problem solved.

  • User profile image

    I used to use something quite similar, however this is not reliable due to variety of reasons.

    print "Content-type: text\n\n";
    print $ENV{REMOTE_ADDR};

    #!/opt/perl/bin/perl -w
    use strict;
    use CGI qw(:standard);
    print header(), start_html("IP Address"),p($ENV{'REMOTE_ADDR'}), end_html();

    I just tested these and got some load balancer or perhaps proxy ip.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.