
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
914
|
Chapter 16: Networking
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 ...