Need help with ping sweep

ssmaynv

New member
Joined
Jan 28, 2009
Messages
3
Location
Reno, NV
Programming Experience
Beginner
I'm very new to the programming world so please allow some fumbling on my part.

I'm trying to put together an app that will do a ping sweep of my classroom so I know the status of my students computers at a glance. Once the app gets a "timed out" response from a ping, every address after that will also receive the same error. If I manually step through the program (F10) it works correctly. I assume the app is moving to quickly and I've tried using timers to slow down the pinging but have not been successful. Below is the code I've modified from another developer in an effort to learn myself. There are 4 ip groups I'm using but only included the first one. If needed I can upload entire app. Thank you for any suggestions.

- S

VB.NET:
Public Class frmMain
    Private mPingAddresses As List(Of String)
    Private grp1Addresses As List(Of String)
    Private grp2Addresses As List(Of String)
    Private grp3Addresses As List(Of String)
    Private grp4Addresses As List(Of String)
    Dim ipCount As Integer
    Dim ipSuccess As Integer
    Dim ipFailed As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Debug.WriteLine("Starting Ping Sweep Application")
        ' Create DataGrid
        Debug.WriteLine(".....Creating DataGrid")
        Me.dgResults.Columns.Add("IP", "IP Adress")
        Me.dgResults.Columns("IP").Width = 100
        Me.dgResults.Columns.Add("result", "Ping Result")
        Me.dgResults.Columns("result").Width = 100
        Me.dgResults.Visible = True

        ' Add IP Addresses to be pinged

        ' Beginning of Group 1
        Debug.WriteLine(".....Creating IP Group 1")
        grp1Addresses = New List(Of String)

        ' Group 1 Satellite
        grp1Addresses.Add("172.16.60.2")
        ' Ft. Brag
        grp1Addresses.Add("147.51.10.1")
        grp1Addresses.Add("147.51.10.2")
        grp1Addresses.Add("10.10.10.1")
        grp1Addresses.Add("10.10.10.2")
        grp1Addresses.Add("10.10.10.3")
        ' Ft. Knox
        grp1Addresses.Add("147.51.20.1")
        grp1Addresses.Add("147.51.20.2")
        grp1Addresses.Add("10.10.20.1")
        grp1Addresses.Add("10.10.20.2")
        grp1Addresses.Add("10.10.20.3")
        ' Ft. Lewis
        grp1Addresses.Add("147.51.30.1")
        grp1Addresses.Add("147.51.30.2")
        grp1Addresses.Add("10.10.30.1")
        grp1Addresses.Add("10.10.30.2")
        grp1Addresses.Add("10.10.30.3")
        ' Bosnia
        grp1Addresses.Add("147.51.40.1")
        grp1Addresses.Add("147.51.40.2")
        grp1Addresses.Add("10.10.40.1")
        grp1Addresses.Add("10.10.40.2")
        grp1Addresses.Add("10.10.40.3")
        ' Turkey
        grp1Addresses.Add("147.51.50.1")
        grp1Addresses.Add("147.51.50.2")
        grp1Addresses.Add("10.10.50.1")
        grp1Addresses.Add("10.10.50.2")
        grp1Addresses.Add("10.10.50.3")
        ' Iraq
        grp1Addresses.Add("147.51.60.1")
        grp1Addresses.Add("147.51.60.2")
        grp1Addresses.Add("10.10.60.1")
        grp1Addresses.Add("10.10.60.2")
        grp1Addresses.Add("10.10.60.3")

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Clear all result items
        Debug.WriteLine("Clearing all variables and results display")
        Me.IPGrp1.Items.Clear()
        Me.IPGrp2.Items.Clear()
        Me.IPGrp3.Items.Clear()
        Me.IPGrp4.Items.Clear()
        ipCount = "0"
        ipFailed = "0"
        strip2.Text = "0"
        strip4.Text = "0"
        strip6.Text = "0"
        progBar.Value = "0"
        dgResults.Rows.Clear()
        progBar.Visible = True

        Debug.WriteLine("Performing Ping Sweep")
        ' Loop through the addresses generating an aysnc ping...
        Debug.WriteLine(".....Looping through IP Group 1")
        For Each ipAddy As String In grp1Addresses
            ' Generate the request
            Dim myPing As New Net.NetworkInformation.Ping()
            ' Add the handler for this request...
            AddHandler myPing.PingCompleted, AddressOf grp1RequestCompleted
            myPing.SendAsync(ipAddy, ipAddy)
            Debug.Write("P")
        Next
        Debug.WriteLine(".....Looping through IP Group 2")
        For Each ipAddy As String In grp2Addresses
            ' Generate the request
            Dim myPing As New Net.NetworkInformation.Ping()
            ' Add the handler for this request...
            AddHandler myPing.PingCompleted, AddressOf grp2RequestCompleted
            myPing.SendAsync(ipAddy, ipAddy)
        Next
        Debug.WriteLine(".....Looping through IP Group 3")
        For Each ipAddy As String In grp3Addresses
            ' Generate the request
            Dim myPing As New Net.NetworkInformation.Ping()
            ' Add the handler for this request...
            AddHandler myPing.PingCompleted, AddressOf grp3RequestCompleted
            myPing.SendAsync(ipAddy, ipAddy)
        Next
        Debug.WriteLine(".....Looping through IP Group 4")
        For Each ipAddy As String In grp4Addresses
            ' Generate the request
            Dim myPing As New Net.NetworkInformation.Ping()
            ' Add the handler for this request...
            AddHandler myPing.PingCompleted, AddressOf grp4RequestCompleted
            myPing.SendAsync(ipAddy, ipAddy)
        Next
    End Sub

    Public Sub grp1RequestCompleted(ByVal sender As Object, ByVal e As Net.NetworkInformation.PingCompletedEventArgs)
        ' When received, add the appropriate entry into the listbox and DataGrid
        If e.Reply.Status = "0" Then
            Me.IPGrp1.Items.Add("IP Address: " & e.UserState.ToString & " Returned Code: " & e.Reply.Status)
            Me.dgResults.Rows.Add(e.UserState.ToString, "Successful")
            ipSuccess = ipSuccess + 1
            strip4.Text = ipSuccess
        ElseIf e.Reply.Status = "11010" Then
            Me.IPGrp1.Items.Add("IP Address: " & e.UserState.ToString & " Returned Code: " & e.Reply.Status)
            Me.dgResults.Rows.Add(e.UserState.ToString, "Request Timed Out")
            ipFailed = ipFailed + 1
            strip6.Text = ipFailed
        ElseIf e.Reply.Status = "11003" Then
            Me.IPGrp1.Items.Add("IP Address: " & e.UserState.ToString & " Returned Code: " & e.Reply.Status)
            Me.dgResults.Rows.Add(e.UserState.ToString, "Host Unreachable")
            ipFailed = ipFailed + 1
            strip6.Text = ipFailed
        End If
        ipCount = ipCount + 1
        strip2.Text = ipCount
        progBar.Value = progBar.Value + 1
    End Sub
 
I can't see any problems with the ping functionality of the code.

e.Reply.Status is not type String, it is enumeration type IPStatus, you would compare it like this:
VB.NET:
If e.Reply.Status = IPStatus.Success Then
or a Select Case
VB.NET:
Select Case e.Reply.Status
    Case IPStatus.Success

    Case IPStatus.TimedOut

    Case IPStatus.DestinationHostUnreachable

End Select
Turning on Option Strict will help you write better code and in some cases avoid errors due to unexpected implicit type conversions.

As for you various "strip.Text" I don't understand your logic of those, but you should get correct results in the grid rows.

I assume the app is moving to quickly
The different Ping instances are not related to each other, apart from using the same network adapter and connection, so one timeout will not affect the others as long as the network connection works properly.
 
JohnH,

I took your advice and changed the code and it's much easier to follow and performs better.

You mentioned the network connection and that got me thinking about our wireless link. I switched to a physical connection and now everything works perfectly. I guess sometimes it takes fresh eyes for something to change. Thanks for all your help!
 
Back
Top