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) </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;
}
}