Tutorial: Ping Network Device


Introduction: I often see many people asking how to do a ‘ping’ in VB.NET. It is actually quite simple and I utilize it in my IRIS project. This tutorial will teach you how to ping a network device and view the pings’ results.

What you will need: Any version of Visual Studio (Express is fine), a few free minutes and your energy drink of choice.

First, what does it mean to “ping?” A ping is useful when you need to know if a network device is communicating with other network devices. This could be a computer, phone, IP Camera, or pretty much anything that has networking capabilities and an IP address.

Author Note: A Ping may not always provide a good indication if a device is communicating or not. Many System Administrators will disable ICMP messages to “hide” a machine on the network from malicious entities.

Let’s start by opening a new project and adding a blank Windows form. We will add a Textbox, a Button and a Label control to the form like so:

Ping Form Layout

We will name the controls:

ipTextbox
resultLabel
pingButton

What we are going to do is have the user enter an IP address (format xxx.xxx.xxx.xxx) into the Textbox and clicking the button will ping the IP. The results of the ping ("Online" or "Offline") will then show in the label.

Let’s double-click the pingButton to create and enter its Click Event code block. We are going to call a function, IsDestinationReachable, which will accept the IP (provided from ipTextbox) as a string argument. (We will code this function later in the tutorial) This function will return reachable, a boolean value that tells us True (Online) or False (Offline). We are going to test the value of reachable and if it is True, set the Label’s Text property to "Online," and if it is False, set it to "Offline." So our code will be:

    Private Sub pingButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pingButton.Click
        If IsDestinationReachable(ipTextbox.Text) Then
            resultLabel.Text = "Online"
        Else
            resultLabel.Text = "Offline"
        End If
    End Sub

Now that we have our button coded, let’s work on that function. We’ll begin by declaring it a Private function and naming it. It will accept the string By Value and store it in hostnameOrAddress. We will declare our reachable boolean variable and initialize it to False.

Now we will enter what’s called a Try/Catch block. This will alert us if any exceptions (errors) show up when the ping is taking place. We are going to use the Network class to send and receive status information.

My.Computer.Network.IsAvailable
My.Computer.Network.Ping('ip, 'timeout)

Here is the IsDestinationReachable function:

    Private Function IsDestinationReachable(ByVal hostnameOrAddress As String) As Boolean
        Dim reachable As Boolean = False
        Try
            reachable = My.Computer.Network.IsAvailable AndAlso _
                        My.Computer.Network.Ping(hostnameOrAddress, 10000)
        Catch pingException As System.Net.NetworkInformation.PingException
        Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException
            ' Fail silently and return false
        End Try
        Return reachable
    End Function

We start by declaring it a Private function and naming it. The reachable boolean gets declared and initialized and then we enter our Try/Catch block. We then use the Ping method of the Network class by sending it our IP (provided from ipTextbox.Text) and a timeout amount, which I’ve made 10000ms above. The result of this ping gets stored in the reachable variable, either True or False. When we return back to the method call in the button’s click event, we test the value of reachable. If the result was True, we set our Label’s text to “Online”, if the result was False, we set the text to “Offline.”

At this point, you should be able to compile and run your program without any errors. (cross your fingers :))

Let’s start by entering a known good IP address. We’ll use 127.0.0.1 (Localhost, which will return Online). When you enter this into the Textbox and click the button, after a second or two, the Label will display “Online.”

Ping Success

So far so good!

Ok, now let’s try a bad IP address. We’ll use 100.100.100.256 (the last octet is outside the 255 range so will return Offline). Go ahead and enter this IP and click your button and see what your Label says now. If all went correct, you should see the screen below:

Ping fail

Let’s take a look at the code all together now:

Public Class mainForm

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Nothing to do here
    End Sub

    Private Sub pingButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pingButton.Click
        If IsDestinationReachable(ipTextbox.Text) Then
            resultLabel.Text = "Online"
        Else
            resultLabel.Text = "Offline"
        End If
    End Sub

    Private Function IsDestinationReachable(ByVal hostnameOrAddress As String) As Boolean
        Dim reachable As Boolean = False
        Try
            reachable = My.Computer.Network.IsAvailable AndAlso _
                        My.Computer.Network.Ping(hostnameOrAddress, 10000)
        Catch pingException As System.Net.NetworkInformation.PingException
        Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException
            ' Fail silently and return false
        End Try
        Return reachable
    End Function

End Class

That wasn’t so bad now was it? You can use this ping functionality a number of different ways. You can have the user supply the IP address, you can hard code it, you can load it from a text file or a database, the list goes on! See what you can come up with and report back!

As always, let me know if you found the post helpful. Comments, good or bad (but I like the good ones :)) are welcome!

3 thoughts on “Tutorial: Ping Network Device

  1. I just turned AutoSize off and filled whatever space I had left on the form. Others might want to put more text than what I have there.

    Simply for demo purposes 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *