BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Network and Internet
.NET 2.0+

Pinging a Remote Computer

When developing networked applications that rely on a remote computer being available, it is important to be able to check that the other system is operable. Using the .NET framework's Ping class, an ICMP echo request can be sent for this purpose.

Ping Class

The Ping class was introduced in the .NET framework version 2.0, in the System.Net.NetworkInformation namespace. The class provides the functionality required to check if a remote computer is accessible over a network or via the Internet.

The Ping class includes two methods that send an Internet Control Message Protocol (ICMP) echo request packet and wait for a reply. These are the synchronous Send method and the asynchronous SendAsync method. In this tip we will concentrate on the former.

The Send method has several overloaded versions. The simplest of these accepts a single parameter containing the name or IP address of the target computer as a string. Adding an integer parameter allows the specification of a timeout value in milliseconds to override the default value of five seconds. If a response is not received within the specified time, the ping activity is halted and a timeout status is reported.

Ping ping = new Ping();
ping.Send("localhost");         // Default timeout
ping.Send("127.0.0.1", 10000);  // Ten second timeout

The PingReply Class

The Ping class's Send method returns an instance of the PingReply class. The returned object provides several properties, two of which are important to determining the availability of the remote computer. The Status property holds the overall result of the ping operation. If this enumeration value is set to "Success", the remote computer is available. If the status is any other value then the ping failed, suggesting that the target system may be unavailable or blocking echo requests using a firewall.

If the ping operation is successful, the performance of the link can be checked by examining the RoundtripTime property of the PingReply object. This property is an integer value that holds the duration of the operation in milliseconds.

The following example tests the Ping.Send method by pinging the local computer. The operation should be almost instantaneous, hence the zero millisecond round trip time.

Ping ping = new Ping();
PingReply reply = ping.Send("localhost");
Console.WriteLine(reply.Status);            // Outputs "Success"
Console.WriteLine(reply.RoundtripTime);     // Outputs "0ms"

IPAddress Class

When your program only needs to ping using IP addresses, the IPAddress class in the System.Net namespace can be used instead of a string. This class is designed to hold valid IP addresses only.

byte[] ip = new byte[] { 127, 0, 0, 1 };
IPAddress ipAddress = new IPAddress(ip);
Ping ping = new Ping();
PingReply reply = ping.Send(ipAddress);
Console.WriteLine(reply.Status);            // Outputs "Success"
Console.WriteLine(reply.RoundtripTime);     // Outputs "0ms"
9 March 2008