+ Reply to Thread
Results 1 to 2 of 2

Thread: Get the public ip address of a computer

  1. #1

    Get the public ip address of a computer

    I'd like to get the Public IP Address of a computer , how can i do that ?
    I can only get the Local IP Address
    Last edited by csharpmaster; 10-31-2012 at 04:34 AM.

  2. #2
    Tell me , whenever you want to know your public IP online , what do you actually do ?
    You go to whatismyip.com and get it right ?
    So that`s what we exactly are going to do , here are the steps :
    1- Go to http://checkip.dyndns.org/
    2- read the source code which has the ip address
    3- we will extract the IP address format using the substring method

    Here is the code :
    Code:
            static string GetPublicIP()
            {
                String direction = "";
                WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
                using (WebResponse response = request.GetResponse())
                using (StreamReader stream = new StreamReader(response.GetResponseStream()))
                {
                    direction = stream.ReadToEnd();
                }
    
                //Search for the ip in the html
                int first = direction.IndexOf("Address: ") + 9;
                int last = direction.LastIndexOf("</body>");
                direction = direction.Substring(first, last - first);
    
                return direction;
            }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts