Small module written to replicate the ping functionality in C# code
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; | |
} |