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:
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: