[/COLOR][/FONT]using[/COLOR] System;
[COLOR=blue]using[/COLOR] System.Text;
[COLOR=blue]using[/COLOR] System.Net;
[COLOR=blue]using[/COLOR] System.Net.NetworkInformation;
[COLOR=blue]using[/COLOR] System.ComponentModel;
[COLOR=blue]using[/COLOR] System.Threading;
[COLOR=blue]namespace[/COLOR] Examples.System.Net.NetworkInformation.PingTest
{
[COLOR=blue]public[/COLOR] [COLOR=blue]class[/COLOR] PingExample
{
[COLOR=blue]public[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] Main ([COLOR=blue]string[/COLOR][] args)
{
[COLOR=blue]if[/COLOR] (args.Length == 0)
throw [COLOR=blue]new[/COLOR] ArgumentException ("Ping needs a host or IP Address.");
[COLOR=blue]string[/COLOR] who = args[0];
AutoResetEvent waiter = [COLOR=blue]new[/COLOR] AutoResetEvent ([COLOR=blue]false[/COLOR]);
Ping pingSender = [COLOR=blue]new[/COLOR] Ping ();
[COLOR=green]// When the PingCompleted event is raised,[/COLOR]
[COLOR=green]// the PingCompletedCallback method is called.[/COLOR]
pingSender.PingCompleted += [COLOR=blue]new[/COLOR] PingCompletedEventHandler (PingCompletedCallback);
[COLOR=green]// Create a buffer of 32 bytes of data to be transmitted.[/COLOR]
[COLOR=blue]string[/COLOR] data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
[COLOR=green]// Wait 12 seconds for a reply.[/COLOR]
[COLOR=blue]int[/COLOR] timeout = 12000;
[COLOR=green]// Set options for transmission:[/COLOR]
[COLOR=green]// The data can go through 64 gateways or routers[/COLOR]
[COLOR=green]// before it is destroyed, and the data packet[/COLOR]
[COLOR=green]// cannot be fragmented.[/COLOR]
PingOptions options = [COLOR=blue]new[/COLOR] PingOptions (64, [COLOR=blue]true[/COLOR]);
Console.WriteLine ("Time to live: {0}", options.Ttl);
Console.WriteLine ("Don't fragment: {0}", options.DontFragment);
[COLOR=green]// Send the ping asynchronously.[/COLOR]
[COLOR=green]// Use the waiter as the user token.[/COLOR]
[COLOR=green]// When the callback completes, it can wake up this thread.[/COLOR]
pingSender.SendAsync(who, timeout, buffer, options, waiter);
[COLOR=green]// Prevent this example application from ending.[/COLOR]
[COLOR=green]// A real application should do something useful[/COLOR]
[COLOR=green]// when possible.[/COLOR]
waiter.WaitOne ();
Console.WriteLine ("Ping example completed.");
}
[COLOR=blue]private[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
[COLOR=green]// If the operation was canceled, display a message to the user.[/COLOR]
[COLOR=blue]if[/COLOR] (e.Cancelled)
{
Console.WriteLine ("Ping canceled.");
[COLOR=green]// Let the main thread resume. [/COLOR]
[COLOR=green]// UserToken is the AutoResetEvent object that the main thread [/COLOR]
[COLOR=green]// is waiting for.[/COLOR]
((AutoResetEvent)e.UserState).Set ();
}
[COLOR=green]// If an error occurred, display the exception to the user.[/COLOR]
[COLOR=blue]if[/COLOR] (e.Error != [COLOR=blue]null[/COLOR])
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ());
[COLOR=green]// Let the main thread resume. [/COLOR]
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
DisplayReply (reply);
[COLOR=green]// Let the main thread resume.[/COLOR]
((AutoResetEvent)e.UserState).Set();
}
[COLOR=blue]public[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] DisplayReply (PingReply reply)
{
[COLOR=blue]if[/COLOR] (reply == [COLOR=blue]null[/COLOR])
[COLOR=blue]return[/COLOR];
Console.WriteLine ("ping status: {0}", reply.Status);
[COLOR=blue]if[/COLOR] (reply.Status == IPStatus.Success)
{
Console.WriteLine ("Address: {0}", reply.Address.ToString ());
Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
}
}
}