How to Check Local IP Address


Check IP Address Is Local Or Not

The following code snippet will tell you given IP address is Local or not.

public static bool IsLocalIpAddress(string host)  
{  
  try  
  { // get host IP addresses  
    IPAddress[] hostIPs = Dns.GetHostAddresses(host);  
    // get local IP addresses  
    IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());  
  
    // test if any host IP equals to any local IP or to localhost  
    foreach (IPAddress hostIP in hostIPs)  
    {  
      // is localhost  
      if (IPAddress.IsLoopback(hostIP)) return true;  
      // is local address  
      foreach (IPAddress localIP in localIPs)  
      {  
        if (hostIP.Equals(localIP)) return true;  
      }  
    }  
  }  
  catch { }  
  return false;  
}  
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s