Check Local IP Address


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.