Monday, 24 March 2014

how to find the ip address of a computer c#

There are different ways to get the IP address of client computers;

Exploring the two way to get the IP address;

First is to find the LAN IP of computer address; which is in part of the LAN.

//Getting the lan IP of client
private string GetClientComputerLANIP()
{
    string strHostName = System.Net.Dns.GetHostName();

    string IPADDRESS = string.Empty;
    IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
    
    foreach (IPAddress ipAddress in ipEntry.AddressList)
    {
        if (ipAddress.AddressFamily.ToString() == "InterNetwork")
        {
            IPADDRESS =  ipAddress.ToString();
        }
    }

    return IPADDRESS;

}


Get Internet IP address;

There is no any particular inbuilt method in .Net to get the IP address ; Thats why need to call dyndns.org

//Getting the omputer IP address
private string GetSystemInternetIP()
{
    WebRequest request = WebRequest.Create("http://checkip.dyndns.org");  // check IP using DynDNS's service
    WebResponse response = request.GetResponse();
    StreamReader stream = new StreamReader(response.GetResponseStream());
   
    request.Proxy = null; //  set Proxy to null, to drastically INCREASE the speed of request

    // read complete response
    string ipAddress = stream.ReadToEnd();

    // replace everything and keep only IP
    return ipAddress.
        Replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", string.Empty).
        Replace("</body></html>", string.Empty);

}

No comments:

Post a Comment

Others

You will like to read it