In our development project, we often encounter the problem of host ping. Now I have encapsulated a method to ping the host,
The Code is as follows:
/// <Summary>
/// Ping the specified host to see if it can be pinged
/// </Summary>
/// <Param name = "Address"> (host Address: www.2cto.com) </param>
/// <Param name = "TimeOut"> (TimeOut time, default: 1 s) </param>
/// <Returns> True if a response is already ed, false otherwise </returns>
Public static bool PingHost (string Address, int TimeOut = 1000)
{
Using (System. Net. NetworkInformation. Ping PingSender = new System. Net. NetworkInformation. Ping ())
{
PingOptions Options = new PingOptions ();
Options. DontFragment = true;
String 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;
}
}
From weizhiai12's column