using System;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
public static void Main (string[] args)
{
if (args.Length == 0)
throw new ArgumentException ("Ping needs a host or IP Address.");
string who = args[0];
AutoResetEvent waiter = new AutoResetEvent (false);
Ping pingSender = new Ping ();
pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 12000;
PingOptions options = new PingOptions (64, true);
Console.WriteLine ("Time to live: {0}", options.Ttl);
Console.WriteLine ("Don't fragment: {0}", options.DontFragment);
pingSender.SendAsync(who, timeout, buffer, options, waiter);
waiter.WaitOne ();
Console.WriteLine ("Ping example completed.");
}
private static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
if (e.Cancelled)
{
Console.WriteLine ("Ping canceled.");
((AutoResetEvent)e.UserState).Set ();
}
if (e.Error != null)
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ());
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
DisplayReply (reply);
((AutoResetEvent)e.UserState).Set();
}
public static void DisplayReply (PingReply reply)
{
if (reply == null)
return;
Console.WriteLine ("ping status: {0}", reply.Status);
if (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);
}
}
}
}
Source:
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs.90%29.aspx[
^]