Question Application to PING IP addresses runs slow?????????????

Trickyzxr

Member
Joined
Oct 3, 2011
Messages
5
Programming Experience
Beginner
Hi,

Mr Newbie to VB.Net here.

I have grab various snippets of code to build a small app to allow the user to input the 1st three octets of an IP address and then click a start button which sets off a timer to run the loop for pinging the start IP address and then incrementing the 4th octect and continually doing this until the stop button is pressed. The result of the ping and the IP address are sent to a textbox. I also have a date and time timer running. I have had to use some code to convert the IP address from bytes to a string which comes out reversed and then split it and put it in the correct order.

The problem I am having is that once I click on the start button depending on the value of I, say 1 to 254, it takes ages to populate the textbox with the results of the ping. It seems as though it is waiting for all the IP addresses to be pinged and then write the results to the textbox.

Please could any one tell me a way of overcoming this so my code pings one address, displays the result in the text box, increments the 4th octect by one, pings the address and so on repeating for ever? As I say I am new to vb and there is probably a lot cleaner way to achieve all of this but if I can over come this one glitch that would be great. Thanks in advance for any assistance given.

My Code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        'Gets values of IP address from text boxes
        Dim octect4a As Integer
        octect4a = 0
        Dim octect1 As String
        octect1 = octet1.Text
        Dim octect2 As String
        octect2 = octet2.Text
        Dim octect3 As String
        octect3 = octet3.Text
        Dim octect4 As String
        octect4 = octect4a

        ' Use Parse() to convert from the starting address string to an IPAddress.
        Dim startAddress As IPAddress = IPAddress.Parse(octect1 + "." + octect2 + "." + octect3 + "." + octect4)
        ' This is a loop variable that will be updated each pass.
        Dim currentAddress As IPAddress = startAddress
        For i As Integer = 1 To 10
            ' We need the integral value of the IP. The Address property is deprecated. Use GetAddressBytes() and
            ' convert the bytes to a value. If you are using IPv4, this should be 4 bytes, 32 bits, only Integer is needed.
            Dim addressBytes() As Byte = currentAddress.GetAddressBytes()
            Dim addressValue As Integer = BitConverter.ToInt32(addressBytes, 0)
            ' The bytes come out in network order; we don't want to figure out if that's backwards or not. Use this method
            ' to convert to whatever the host machine uses.
            addressValue = IPAddress.NetworkToHostOrder(addressValue)
            ' Increment to the next IP; probably want to turn off overflow checking!
            addressValue += 1
            ' We're ready to go back to bytes; convert the number back to network order
            addressValue = IPAddress.HostToNetworkOrder(addressValue)
            ' Turn it back into bytes, create the new IP, and print it.
            addressBytes = BitConverter.GetBytes(addressValue)
            currentAddress = New IPAddress(addressBytes)

            'Split addressvalue in to 4 strings in order to reverse IP address
            Dim message2 As String = IPAddress.Parse(addressValue).ToString()
            Dim ip4 As String
            Dim ip3 As String
            Dim ip2 As String
            Dim ip1 As String
            ip4 = Split(message2, ".")(3)
            ip3 = Split(message2, ".")(2)
            ip2 = Split(message2, ".")(1)
            ip1 = Split(message2, ".")(0)
            ' Creat the Ping
            If My.Computer.Network.Ping(ip4 + "." + ip3 + "." + ip2 + "." + ip1) Then
                ' If ping response OK print below in Textbox
                msg2("Ping: " + ip4 + "." + ip3 + "." + ip2 + "." + ip1 + "  OK      " + Now.ToShortDateString() + "     " + Now.ToLongTimeString() & vbCrLf)
            Else
                ' If ping response fails print below in Textbox
                msg2("Ping: " + ip4 + "." + ip3 + "." + ip2 + "." + ip1 + "  Failed  " + Now.ToShortDateString() + "    " + Now.ToLongTimeString() & vbCrLf)
            End If
           
        Next
    End Sub
 
Last edited by a moderator:
Is there any options 'ping' ?

The command prompt ping does 4 pings as default, so thats a little different, if you told it to just do 1 ping it would be quite fast.
 
So how do I tell it to do just one ping of each IP address then increment to the next IP address?

Also just found a problem that when the value of I gets to 128 the app fails saying invalid IP address. Any ideas what this could be please?
 
I don't mean to be rude, but did you type the code you have listed?

Sorry I just read you put together snippets..
 
Yes i grabbed snippets from here and there to achieve the listed code. Not bad for a newbie learning myself over a month or so!!!:cheerful:
 
VB.NET:
 MsgBox(My.Computer.Network.Ping("192.168.0.1", 150))


in your code, type in my.computer.network.ping( and then you will see the options avaliable.

So looking at the code above, all you now have to do is find a way to increment the IP address (loops?) and then stick the results in something as it goes (listbox?)

I don't know how to build the ip address value off the top of my head, but im sure you will get there if you take logical steps :)
 
The problem I am having is that once I click on the start button depending on the value of I, say 1 to 254, it takes ages to populate the textbox with the results of the ping. It seems as though it is waiting for all the IP addresses to be pinged and then write the results to the textbox.

with this problem, you need a multi threader, set like a number of threads to create (10 threads) at a time, so its 10 IP at a time, but the applications would not hang-up?
if thats what you want and i guess so..

here's a snippet for that one.
 
'this will be declared as Class variables
Delegate Sub MultipleThreadPinger(ByVal index As Integer)
    Dim Pinger As MultipleThreadPinger
Dim currentIndex as Integer = 0

'Button1 Click event.

           For i = 0 To 9 'or so, if where you want to put the source of it always subtract 1
                Pinger = New MultipleThreadPinger(AddressOf CheckProxy)
                Pinger.BeginInvoke(currentIndex, Nothing, Nothing)
                currentIndex += 1
            Next


Sub CheckProxy(index as Integer)

Try
'Do whatever codes you want here,


'then here...
'.....write a state like
if Ping..blahblah is Ok then
                If currentIndex < 9 Then
                    Pinger.BeginInvoke(currentIndex, Nothing, Nothing)
                    currentIndex += 1
                End If
else 'if ping failed 
                If currentIndex < 9 Then
                    Pinger.BeginInvoke(currentIndex, Nothing, Nothing)
                    currentIndex += 1
                End If

end if

Catch ex as Exception
                If currentIndex < 9 Then
                    Pinger.BeginInvoke(currentIndex, Nothing, Nothing)
                    currentIndex += 1
                End If
End Try

End Sub

Basically, what the code does is, it will generate a threads upto whatever value you want. check the IP in incremental mode, since the kickoff index was based on the currentIndex w/c after you have invoke it above, it will automatically increase by 1, so , if you set like 9, it will have 10 threads,

This is best for checking proxies and IP's or whatever multiThread

With this codes you can create even 20threads at a time without worrying on the application perfomance that "My Application hanged"

hope this helps you :)
 
Last edited:
thanks very much for you post. I will take a deeper look later and give it a try and let you know how i get on. Thanks once again.
 
Back
Top