общий смысл без колбэков такой
Код:
Ping pingSender = new Ping ();
PingReply reply = pingSender.Send ("www.wtf.com");
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);
}
else
{
Console.WriteLine (reply.Status);
}
с колбэками, как вариант примерно так
Код:
AutoResetEvent waiter = new AutoResetEvent (false);
Ping pingSender = new Ping ();
pingSender.PingError += new PingErrorEventHandler(PingErrorCallback);
pingSender.PingStarted += new PingStartedEventHandler(PingStartedCallback);
pingSender.PingResponse += new PingResponseEventHandler(PingResponseCallback);
pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 12000;
PingOptions options = new PingOptions (64, true);
pingSender.SendAsync(who, timeout, buffer, options, waiter);
waiter.WaitOne ();
...
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;
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);
}
((AutoResetEvent)e.UserState).Set();
}
но это шутка по поводу
текстового редактора студии. вам вообще как минимум
сюда сначала нужно заглянуть. только смотря чего от пинга требуется, можно легко и в пару сот строк кода не влезть, если писать мультиплатформеный на сокетах.