Programmatically Ping a Networked Device

I would say Ping is one of the most popular methods used to troubleshoot network issue. When a networked device or server goes down, one of the first things a network engineer or a system admininistrator do is try to ping it and see if it can be reached.

This blog will show you two ways to ping a device so you can integrate the ping feature into your own application.

The first and easiest way to issue a ping command is to call the Ping method in the My.Computer.Network class:

If my.Computer.Network.Ping("www.google.com") Then
MsgBox("device up")
Else
MsgBox("device down")
End If

The drawback of this method is that it only gives you a boolean as the result to indicate whether the device is up or down. It doesn’t provide any other details like RTT, TTL or any error code for that matter.

The second way is to call the Ping.Send() method in the System.Net.NetworkInformation namespace which returns a PingReply object through which you could get more detailed information about the ping reply:

Dim myPing As New System.Net.NetworkInformation.Ping
Dim PR As System.Net.NetworkInformation.PingReply
PR = myPing.Send("www.yahoo.com")
If PR.Status = IPStatus.Success Then
MsgBox("Reply from " & PR.Address.ToString & ": BYTES=" & PR.Buffer.Length & " TIME<" & PR.RoundtripTime & "ms TTL=" & PR.Options.Ttl) Else MsgBox(PR.Status.ToString) End If

There you have it. Two ways of pinging a networked device. If you just need to check and see if a device is up or down, the first method might do just fine; however, if you need to gather some other statistics then obviously, the second one is the better choice.

I recommend that you read up on the PingReply object to get more information from the ping reply. If you wanted to implement some sort of Ping functionality as in Pinkie's then that's the way to go.