Posted By

falconzy on 11/25/10


Tagged

DNS


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

thastyle


Check Local IP Address


 / Published in: C#
 

  1. private bool CheckLocalAdress(string host)
  2. {
  3. try
  4. { // get host IP addresses
  5. IPAddress[] hostIPs = Dns.GetHostAddresses(host);
  6. // get local IP addresses
  7. IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
  8.  
  9. // test if any host IP equals to any local IP or to localhost
  10. foreach (IPAddress hostIP in hostIPs)
  11. {
  12. // is localhost
  13. if (IPAddress.IsLoopback(hostIP)) return true;
  14. // is local address
  15. foreach (IPAddress localIP in localIPs)
  16. {
  17. if (hostIP.Equals(localIP)) return true;
  18. }
  19. }
  20. return false;
  21. }
  22. catch (Exception ex)
  23. { throw new Exception(ex.Message.ToString()); }
  24. }

Report this snippet  

You need to login to post a comment.