See Also: Ping Members
Applications use the System.Net.NetworkInformation.Ping class to detect whether a remote computer is reachable.
Network topology can determine whether System.Net.NetworkInformation.Ping can successfully contact a remote host. The presence and configuration of proxies, network address translation (NAT) equipment, or firewalls can prevent System.Net.NetworkInformation.Ping from succeeding. A successful System.Net.NetworkInformation.Ping indicates only that the remote host can be reached on the network; the presence of higher level services (such as a Web server) on the remote host is not guaranteed.
This class provides functionality similar to the Ping.exe command line tool. The erload:System.Net.NetworkInformation.Ping.Send and erload:System.Net.NetworkInformation.Ping.SendAsync methods send an Internet Control Message Protocol (ICMP) echo request message to a remote computer and waits for an ICMP echo reply message from that computer. For a detailed description of ICMP messages, see RFC 792, available at tp://www.ietf.org/.
The following types are used with the System.Net.NetworkInformation.Ping class and are described in detail below.
System.Net.NetworkInformation.IPStatus |
Defines status codes that describe the outcome of an ICMP echo request message. |
System.Net.NetworkInformation.PingOptions |
Allows you to configure or retrieve the settings that control how many times the request packet can be forwarded (PingOptions.Ttl), and whether it can be fragmented (PingOptions.DontFragment ). |
System.Net.NetworkInformation.PingReply |
Contains the results of an ICMP echo request. |
System.Net.NetworkInformation.PingException |
Thrown if an unrecoverable error occurs. |
System.Net.NetworkInformation.PingCompletedEventArgs |
Contains the data associated with Ping.PingCompleted events, which are raised when a erload:System.Net.NetworkInformation.Ping.SendAsync call completes or is canceled. |
System.Net.NetworkInformation.PingCompletedEventHandler |
The delegate that provides the callback method invoked when a erload:System.Net.NetworkInformation.Ping.SendAsync call completes or is canceled. |
The erload:System.Net.NetworkInformation.Ping.Send and erload:System.Net.NetworkInformation.Ping.SendAsync methods return the reply in a System.Net.NetworkInformation.PingReply object. The PingReply.Status property returns an System.Net.NetworkInformation.IPStatus value to indicate the outcome of the request.
When sending the request, you must specify the remote computer. You can do this by providing a host name string, an IP address in string format, or an System.Net.IPAddress object.
You can also specify any of the following types of information:
Data to accompany the request. Specifying buffer allows you to learn the amount of time required for a packet of a particular size to travel to and from the remote host and the maximum transmission unit of the network path. (See the erload:System.Net.NetworkInformation.Ping.Send or erload:System.Net.NetworkInformation.Ping.SendAsync overloads that take a buffer parameter.)
Whether the ICMP Echo packet can be fragmented in transit. (See the PingOptions.DontFragment property and the erload:System.Net.NetworkInformation.Ping.Send or erload:System.Net.NetworkInformation.Ping.SendAsync overloads that take an options parameter.)
How many times routing nodes, such as routers or gateways, can forward the packet before it either reaches the destination computer or is discarded. (See PingOptions.Ttl and the erload:System.Net.NetworkInformation.Ping.Send or erload:System.Net.NetworkInformation.Ping.SendAsync overloads that take an options parameter.)
The time limit within which the reply must be received. (See the erload:System.Net.NetworkInformation.Ping.Send or erload:System.Net.NetworkInformation.Ping.SendAsync overloads that take a timeout parameter.
The System.Net.NetworkInformation.Ping class offers both synchronous and asynchronous methods for sending the request. If your application should block while waiting for a reply, use the erload:System.Net.NetworkInformation.Ping.Send methods; these methods are synchronous. If your application should not block, use the asynchronous erload:System.Net.NetworkInformation.Ping.SendAsync methods. A call to erload:System.Net.NetworkInformation.Ping.SendAsync executes in its own thread that is automatically allocated from the thread pool. When the asynchronous operation completes, it raises the Ping.PingCompleted event. Applications use a System.Net.NetworkInformation.PingCompletedEventHandler delegate to specify the method that is called for Ping.PingCompleted events. You must add a System.Net.NetworkInformation.PingCompletedEventHandler delegate to the event before calling erload:System.Net.NetworkInformation.Ping.SendAsync. The delegate's method receives a System.Net.NetworkInformation.PingCompletedEventArgs object that contains a System.Net.NetworkInformation.PingReply object that describes the result of the erload:System.Net.NetworkInformation.Ping.SendAsync call.
You cannot use the same instance of the System.Net.NetworkInformation.Ping class to generate multiple simultaneous ICMP Echo requests. Calling erload:System.Net.NetworkInformation.Ping.Send while a erload:System.Net.NetworkInformation.Ping.SendAsync call is in progress or calling erload:System.Net.NetworkInformation.Ping.SendAsync multiple times before all previous calls have completed causes an InvalidOperationException.