How do I use the Ping class?

The InTheHand.Net.NetworkInformation.Ping class allows you to determine if a network path to a particular host is available and whether the host is responding. It doesn't guarantee that a particular service is running on the server (HTTP, FTP etc). You can perform a Ping in a couple of lines of code, the class has been designed to be an exact subset of the equivalent class in the full .NET framework. The following is an example of the simplest ping request using a default payload and timeout settings.

 

InTheHand.Net.NetworkInformation.Ping p = new InTheHand.Net.NetworkInformation.Ping();
InTheHand.Net.NetworkInformation.PingReply reply = p.Send("www.google.com");

if (reply.Status == IPStatus.Success)
{
string message = string.Format("Address: {0}\r\nRoundTrip time: {1}\r\nTime to live: {2}\r\nDon't fragment: {3}\r\nBuffer size: {0}",
reply.Address.ToString(),
reply.RoundtripTime,
reply.Options.Ttl,
reply.Options.DontFragment,
reply.Buffer.Length);

MessageBox.Show(message, "Ping");
}
else
{
MessageBox.Show(reply.Status.ToString(), "Ping");
}

 

The class allows you to further customise the request. You can specify your own data payload for example, or set a different timeout value (the default is 5 seconds).

Feedback and Knowledge Base