In our development projects often encounter problems to ping a host, and now I have a method that encapsulates ping host,
code show as below:
/// <summary>
/// Ping specified host, it can ping/// </ Summary>
/// <param name = "the Address"> (host address) </ param>
/// <param name = "the TimeOut"> (timeout, default: lS) </ param>
/// <Returns> IS True Response Received IF A, otherwise to false </ Returns>
public static BOOL PingHost (the Address String, int the timeOut = 1000)
{
the using (the System.Net = new new System.Net.NetworkInformation.Ping PingSender .NetworkInformation.Ping ())
{
PingOptions the Options new new PingOptions = ();
Options.DontFragment = to true;
String the Data = "Test";
byte[] DataBuffer = Encoding.ASCII.GetBytes(Data);
PingReply Reply = PingSender.Send(Address, TimeOut, DataBuffer, Options);
if (Reply.Status == IPStatus.Success)
return true;
return false;
}
}
Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2012/02/01/2336477.html