Applications use the Ping class to detect whether a remote computer is reachable.
Network topology can determine whether Ping can successfully contact a remote host. The presence and configuration of proxies, network address translation (NAT) equipment, or firewalls can prevent Ping from succeeding. A successful 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 Send and 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 http://www.ietf.org.
The following types are used with the Ping class and are described in detail below.
Type name
|
Description
|
|---|
IPStatus
|
Defines status codes that describe the outcome of an ICMP echo request message.
|
PingOptions
|
Allows you to configure or retrieve the settings that control how many times the request packet can be forwarded (Ttl), and whether it can be fragmented (DontFragment ).
|
PingReply
|
Contains the results of an ICMP echo request.
|
PingException
|
Thrown if an unrecoverable error occurs.
|
PingCompletedEventArgs
|
Contains the data associated with PingCompleted events, which are raised when a SendAsync call completes or is canceled.
|
PingCompletedEventHandler
|
The delegate that provides the callback method invoked when a SendAsync call completes or is canceled.
|
The Send and SendAsync methods return the reply in a PingReply object. The PingReply..::.Status property returns an 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 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 Send or SendAsync overloads that take a buffer parameter.)
Whether the ICMP Echo packet can be fragmented in transit. (See the DontFragment property and the Send or 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 Ttl and the Send or SendAsync overloads that take an options parameter.)
The time limit within which the reply must be received. (See the Send or SendAsync overloads that take a timeout parameter.
The Ping class offers both synchronous and asynchronous methods for sending the request. If your application should block while waiting for a reply, use the Send methods; these methods are synchronous. If your application should not block, use the asynchronous SendAsync methods. A call to SendAsync executes in its own thread that is automatically allocated from the thread pool. When the asynchronous operation completes, it raises the PingCompleted event. Applications use a PingCompletedEventHandler delegate to specify the method that is called for PingCompleted events. You must add a PingCompletedEventHandler delegate to the event before calling SendAsync. The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call.
You cannot use the same instance of the Ping class to generate multiple simultaneous ICMP Echo requests. Calling Send while a SendAsync call is in progress or calling SendAsync multiple times before all previous calls have completed causes an InvalidOperationException.