#author("2020-01-03T15:44:17+09:00","","") #navi(../) * .NETでpingを実現する方法・System.Net.NetworkInformation.Ping [#gab182ea] 本記事は、System.Net.NetworkInformation.Pingを使って、C#, Visual Basic(VB)でpingをするサンプルコードになります。~ System.Net.NetworkInformation.Pingがあるので簡単にpingを作成することができます。~ Pingの結果は、System.Net.NetworkInformation.PingReplyで取得します。~ 機器監視用にpingを発行するプログラムを作りたい場合などに役に立つかもしれませんね。 #htmlinsert(windev-top.html) #contents * 関連サイト [#be30c443] -[[Microsoft | .NET Pingクラス>https://docs.microsoft.com/ja-jp/dotnet/api/system.net.networkinformation.ping?view=netframework-4.8]] -[[Microsoft | .NET PingReply クラス>https://docs.microsoft.com/ja-jp/dotnet/api/system.net.networkinformation.pingreply?view=netframework-4.8]] * Ping, PingReply クラスを使ってPingを実現 [#s1e50dbe] System.Net.NetworkInformation.Ping を newで作成し、Send(ホスト名orIPアドレス)でPingを送信します。~ 応答は、System.Net.NetworkInformation.PingReplyにセットされるので、あとは判定(Status)し取得した情報を表示しています。 ** C# [#e78c2ff7] using System; using System.Net.NetworkInformation; class Program { static void Main(string[] args) { string host; Ping p = new Ping(); PingReply reply; host = @"www.yahoo.co.jp"; reply = p.Send(host); if (reply.Status == IPStatus.Success) { Console.WriteLine("{0} : PING success.", host); Console.WriteLine("応答情報"); Console.WriteLine("アドレス: {0}", reply.Address.ToString()); Console.WriteLine("バッファサイズ: {0}", reply.Buffer.Length); Console.WriteLine("ラウンドトリップタイム[ms]: {0}", reply.RoundtripTime); } else { Console.WriteLine("{0} : PING failed.", host); } host = @"www.google.co.jp"; reply = p.Send(host); if (reply.Status == IPStatus.Success) { Console.WriteLine("{0} : PING success.", host); Console.WriteLine("応答情報"); Console.WriteLine("アドレス: {0}", reply.Address.ToString()); Console.WriteLine("バッファサイズ: {0}", reply.Buffer.Length); Console.WriteLine("ラウンドトリップタイム[ms]: {0}", reply.RoundtripTime); } else { Console.WriteLine("{0} : PING failed.", host); } } } ** Visual Basic(VB) [#q871a163] Imports System Imports System.Net.NetworkInformation Module Program Sub Main(args As String()) Dim host As String Dim p As Ping = New Ping() Dim reply As PingReply host = "www.yahoo.co.jp" reply = p.Send(host) If reply.Status = IPStatus.Success Then Console.WriteLine("{0} : PING success.", host) Console.WriteLine("応答情報") Console.WriteLine("アドレス: {0}", reply.Address.ToString()) Console.WriteLine("バッファサイズ: {0}", reply.Buffer.Length) Console.WriteLine("ラウンドトリップタイム[ms]: {0}", reply.RoundtripTime) Else Console.WriteLine("{0} : PING failed.", host) End If host = "www.google.co.jp" reply = p.Send(host) If reply.Status = IPStatus.Success Then Console.WriteLine("{0} : PING success.", host) Console.WriteLine("応答情報") Console.WriteLine("アドレス: {0}", reply.Address.ToString()) Console.WriteLine("バッファサイズ: {0}", reply.Buffer.Length) Console.WriteLine("ラウンドトリップタイム[ms]: {0}", reply.RoundtripTime) Else Console.WriteLine("{0} : PING failed.", host) End If End Sub End Module * 実行結果 [#c16a51d0] 上記のC#, Visual Basic(VB)の動作結果のキャプチャは以下の通りです。 #ref(01.png) 以上、.NETでPingを簡単に実装するサンプルコードでした。 #htmlinsert(windev-btm.html)