/Ping.cs Secret
Created
December 12, 2016 13:48
Ping IP Addresses using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool Ping (string host, int attempts, int timeout) | |
{ | |
System.Net.NetworkInformation.Ping ping = | |
new System.Net.NetworkInformation.Ping (); | |
System.Net.NetworkInformation.PingReply pingReply; | |
for (int i = 0; i < attempts; i++) | |
{ | |
try | |
{ | |
pingReply = ping.Send (host, timeout); | |
// If there is a successful ping then return true. | |
if (pingReply != null && | |
pingReply.Status == System.Net.NetworkInformation.IPStatus.Success) | |
return true; | |
} | |
catch | |
{ | |
// Do nothing and let it try again until the attempts are exausted. | |
// Exceptions are thrown for normal ping failurs like address lookup | |
// failed. For this reason we are supressing errors. | |
} | |
} | |
// Return false if we can't successfully ping the server after several attempts. | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment