Recipe16.6.Pinging Programmatically


Recipe 16.6. Pinging Programmatically

Problem

You want to check a computer's availability on the network.

Solution

Use the System.Net.NetworkInformation.Ping class to determine if a machine is available. In the TestPing method, an instance of the Ping class is created. A ping request is sent to www.oreilly.com using the Send method. The Send method is synchronous and returns a PingReply that can be examined for the result of the ping. You perform the second ping request asynchronously using the SendAsync method, after hooking up to the Ping class for the PingCompleted event.

 public static void TestPing() {     System.Net.NetworkInformation.Ping pinger =         new System.Net.NetworkInformation.Ping();     PingReply reply = pinger.Send("www.oreilly.com");     DisplayPingReplyInfo(reply);     pinger.PingCompleted += new PingCompletedEventHandler(pinger_PingCompleted);     pinger.SendAsync("www.oreilly.com", "oreilly ping"); } 

The DisplayPingReplyInfo method shows some of the more common items you want to know from a ping, such as the RoundtripTime and the Status of the reply. These can be accessed from those properties on the PingReply.

 private static void DisplayPingReplyInfo(PingReply reply) {     Console.WriteLine("Results from pinging " + reply.Address);     Console.WriteLine("\tFragmentation allowed?: {0}", !reply.Options.DontFragment);     Console.WriteLine("\tTime to live: {0}", reply.Options.Ttl);     Console.WriteLine("\tRoundtrip took: {0}", reply.RoundtripTime);     Console.WriteLine("\tStatus: {0}", reply.Status.ToString()); } 

The event handler for the PingCompleted event is the pinger_PingCompleted method. This event handler follows the usual EventHandler convention of the sender object and event arguments. The argument type for this event is PingCompletedEventArgs. The PingReply can be accessed in the Reply property of the event arguments. If the ping was canceled or an exception was thrown, that information can be accessed via the Cancelled and Error properties.

 private static void pinger_PingCompleted(object sender, PingCompletedEventArgs e) {     PingReply reply = e.Reply;     DisplayPingReplyInfo(reply);     if(e.Cancelled)     {         Console.WriteLine("Ping for " + e.UserState.ToString() + " was cancelled");     }     else if (e.Error != null)     {         Console.WriteLine("Exception thrown during ping: {0}", e.Error.ToString());     } } 

The output from DisplayPingReplyInfo looks like this:

 Results from pinging 208.201.239.37     Fragmentation allowed?: True     Time to live: 39     Roundtrip took: 103     Status: Success 

Discussion

Ping uses an Internet Control Message Protocol (ICMP) echo request message as defined in RFC 792. If a computer is not reached successfully by the ping request, it does not necessarily mean that the computer is unreachable. Many factors can prevent a ping from succeeding aside from the machine being offline. Network topology, firewalls, packet filters, and proxy servers all can interrupt the normal flow of a ping request. By default, the Windows Firewall installed with Windows XP Service Pack 2 disables ICMP traffic, so if you are having difficulty pinging a machine running XP, check the firewall settings on that machine.

See Also

See the "Ping Class," "PingReply Class," and "PingCompleted Event" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net