以下自分用のメモです。
Dobon.NETさんの方で、既にわかりやすく説明されているのですが
一応メモメモ。。
.NETでPingを送信するには、以下のクラスを利用します。
System.Net.NetworkInfomation.Ping
まんまのクラスですね。
で、このPingクラス。送信方法に同期と非同期が選べるようになっています。
以下、それぞれのサンプルです。
#region PingSamples-01
public class PingSamples01 : IExecutable {
public void Execute() {
//
// 同期での送信.
//
string hostName = "localhost";
int timeOut = 3000;
Ping p = new Ping();
PingReply r = p.Send(hostName, timeOut);
if (r.Status == IPStatus.Success) {
Console.WriteLine("Ping.Send() Success.");
} else {
Console.WriteLine("Ping.Send() Failed.");
}
//
// 非同期での送信.
//
hostName = "www.google.com";
object userToken = null;
p.PingCompleted += (s, e) => {
if (e.Cancelled) {
Console.WriteLine("Cancelled..");
return;
}
if (e.Error != null ) {
Console.WriteLine(e.Error.ToString());
return;
}
if (e.Reply.Status != IPStatus.Success) {
Console.WriteLine("Ping.SendAsync() Failed");
return;
}
Console.WriteLine("Ping.SendAsync() Success.");
};
p.SendAsync(hostName, timeOut, userToken);
Thread.Sleep(3000);
}
}
#endregion
非同期送信の場合は、SendAsyncCancelメソッドでキャンセルが行えます。
また、PingOptionsクラスを用いるとTtl (Time To Live)の設定などが行えます。