-
Jun 22nd, 2009, 09:50 PM
#1
Async Ping How To
Console Application demonstrating using Async Ping.
Code:
'KLEINMA
'WWW.VBFORUMS.COM
'THE PING CLASS IS IN THIS NAMESPACE
Imports System.Net.NetworkInformation
Module Module1
Sub Main()
Dim myPingList As New List(Of String) 'A LIST TO HOLD SOME STRINGS
Dim myPing As Ping = Nothing 'THE PING CLASS WE NEED TO USE
Dim timeout As Integer = 4000 'TIMEOUT FOR PING REQUESTS
Dim buffer As Byte() = {0, 10, 20, 30, 40, 50, _
60, 70, 80, 90, 100, _
110, 120, 130, 140, 150, _
160, 170, 180, 190, 200} 'A BUFFER OF SOME DATA TO PASS IN (optional)
'ADD A BUNCH OF THINGS TO PING
myPingList.Add("127.0.0.1")
myPingList.Add("www.google.com")
myPingList.Add("www.baddomain1234567890.com")
myPingList.Add("www.yahoo.com")
myPingList.Add("192.168.1.1")
'LOOP THE STRING LIST OF PLACES WE WANT TO PING
For Each ItemToPing As String In myPingList
'CREATE A NEW PING OBJECT
myPing = New Ping
'ADD EVENT HANDLER FOR THE PING COMPLETED EVENT
'PingResult IS THE SUB ROUTINE BELOW
AddHandler myPing.PingCompleted, _
AddressOf PingResult
'TELL USER WHAT IS BEING PINGED
Console.WriteLine("Sending Async Request To: " & ItemToPing)
'SEND ASYNC PING REQUEST
myPing.SendAsync(ItemToPing, timeout, buffer, ItemToPing)
Next
'WAIT FOR A KEYPRESS TO CLOSE CONSOLE WINDOW
Console.WriteLine("")
Console.WriteLine("Press any key to close")
Console.ReadKey(True)
End Sub
'THE PING RESULT THAT WILL FIRE WHEN A PING COMPLETES
Private Sub PingResult(ByVal sender As Object, ByVal e As System.Net.NetworkInformation.PingCompletedEventArgs)
'OUTPUT TO USER, e.UserState IS WHAT WE PASSED IN AS THE UserToken (ItemToPing) IN THE ABOVE CODE
Console.WriteLine("")
Console.WriteLine("***********************************")
Console.WriteLine("Result is back from: " & e.UserState.ToString)
'IF THERE WAS AN ERROR THEN OUTPUT INFORMATION ABOUT THE ERROR
'OTHERWISE OUTPUT INFORMATION ABOUT THE RESULT OF THE PING REQUEST
'THE EVENTARGS e HAVE A LOT OF INFORMATION IN THE e.Reply VARIABLE
If e.Error IsNot Nothing Then
Console.WriteLine("Error: " & e.Error.Message)
If e.Error.InnerException IsNot Nothing Then
Console.WriteLine("More Info: " & e.Error.InnerException.Message)
End If
Else
Console.WriteLine("Status: " & e.Reply.Status.ToString)
Console.WriteLine("Round Trip Time: " & e.Reply.RoundtripTime.ToString)
Console.WriteLine("Response Buffer Length:" & e.Reply.Buffer.Length.ToString)
End If
Console.WriteLine("***********************************")
Console.WriteLine("")
'THE SENDER IN THE ARGUMENTS IS THE PING OBJECT THAT WE CREATED
'SO NOW REMOVE THE EVENT HANDLER LISTENER AS IT IS NO LONGER NEEDED
'AND CALL Dispose() ON THE PING CLASS TO CLEAN UP UNMANAGED RESOURCES
With DirectCast(sender, Ping)
RemoveHandler .PingCompleted, AddressOf PingResult
.Dispose()
End With
End Sub
End Module
Last edited by kleinma; Jul 3rd, 2009 at 09:48 AM.
-
Jul 6th, 2009, 08:47 AM
#2
Lively Member
Re: Async Ping How To
thanks kleinma, would it be difficult for me to change this code so that the ip's that are hard coded could be stored along with the indivual ping results in a simple table in a ms access database(2003) i.e just two colums: I.P and Ping Result ?
I have the connection string :
Code:
<connectionStrings> <add name="WindowsApplication1.My.MySettings.VBtestConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\VBtest.mdb" providerName="System.Data.OleDb" /> </connectionStrings>
but im at a loss at what to do next ?
Last edited by markhorgan1; Aug 7th, 2009 at 03:21 AM.
-
Oct 30th, 2009, 01:13 PM
#3
New Member
Re: Async Ping How To
Hi kleinma,
Thanks for the post. that was helpful, but am not building a console application and am not pinging once , i need to make a continos ping till i want it to stop. any idea as to make it ping asnych and continuously?
-
Oct 30th, 2009, 03:02 PM
#4
Re: Async Ping How To
The code is not specific to a console application, that is just what I put the code in for this example.
As far as a continous ping, I would imagine you would be able to accomplish that by simply pinging again in the PingResult routine. IE when you get the ping response from the address you are pinging, ping it again. You would just need some logic in there to cancel when in your user interface you perform whatever action would be to cancel the ping operation.
-
Nov 3rd, 2009, 05:43 AM
#5
New Member
Re: Async Ping How To
Hi Kleinma,
THanks for replying. I have been able to work with the codes to a vb project file. i have also been able to take care of the continuos pinging by putting the codes under a timer_tick event. but the problem now is that, it pings only one ip, instead of three that i want it to ping.
Below is the code.
'ADD A BUNCH OF THINGS TO PING
myPingList.Add(txtWANLink.Text)
myPingList.Add(txtLOSlink.Text)
myPingList.Add(txtPHLINK.Text)
'LOOP THE STRING LIST OF PLACES WE WANT TO PING
For Each ItemToPing As String In myPingList
'CREATE A NEW PING OBJECT
myPing = New Ping
'ADD EVENT HANDLER FOR THE PING COMPLETED EVENT
'PingResult IS THE SUB ROUTINE BELOW
AddHandler myPing.PingCompleted, AddressOf PingResult
'TELL USER WHAT IS BEING PINGED
txtlosps.Text = "Sending Async Request To: " & ItemToPing
txtWANps.Text = "Sending Async Request To: " & ItemToPing
txtPHps.Text = "Sending Async Request To: " & ItemToPing
'SEND ASYNC PING REQUEST
myPing.SendAsync(ItemToPing, timeout, buffer, ItemToPing)
Next
'WAIT FOR A KEYPRESS TO CLOSE CONSOLE WINDOW
End Sub
'THE PING RESULT THAT WILL FIRE WHEN A PING COMPLETES
Private Sub PingResult(ByVal sender As Object, ByVal e As System.Net.NetworkInformation.PingCompletedEventArgs)
""'IF THERE WAS AN ERROR THEN OUTPUT INFORMATION ABOUT THE ERROR
'''OTHERWISE OUTPUT INFORMATION ABOUT THE RESULT OF THE PING REQUEST
'""THE EVENTARGS e HAVE A LOT OF INFORMATION IN THE e.Reply VARIABLE
If e.Error IsNot Nothing Then
Console.WriteLine("Error: " & e.Error.Message)
If e.Error.InnerException IsNot Nothing Then
Console.WriteLine("More Info: " & e.Error.InnerException.Message)
End If
Else
txtWANPs.Text &= "Status: " & e.Reply.Status.ToString & "Round Trip Time: " & e.Reply.RoundtripTime.ToString & "Response Buffer Length:" & e.Reply.Buffer.Length.ToString
End If
'THE SENDER IN THE ARGUMENTS IS THE PING OBJECT THAT WE CREATED
'SO NOW REMOVE THE EVENT HANDLER LISTENER AS IT IS NO LONGER NEEDED
'AND CALL Dispose() ON THE PING CLASS TO CLEAN UP UNMANAGED RESOURCES
With DirectCast(sender, Ping)
RemoveHandler .PingCompleted, AddressOf PingResult
.Dispose()
End With
End Sub
-
Dec 4th, 2009, 09:39 AM
#6
New Member
Re: Async Ping How To
Hi Kleinma
I needed to do this for a few hundred IP addresses, and found your code. It works great, but since the number of IP addresses to ping is so large, I lose some of the info from the console session. I tried to make changes to write the information into a text file, but it doesn't output the results. I am relatively new to VB.NET, so I was wondering if you could please point out the flaw(s) in my code:
Code:
'KLEINMA
'WWW.VBFORUMS.COM
'THE PING CLASS IS IN THIS NAMESPACE
Imports System.Net.NetworkInformation
Module Module1
Sub Main()
Dim myPingList As New List(Of String) 'A LIST TO HOLD SOME STRINGS
Dim myPing As Ping = Nothing 'THE PING CLASS WE NEED TO USE
Dim timeout As Integer = 4000 'TIMEOUT FOR PING REQUESTS
Dim buffer As Byte() = {0, 10, 20, 30, 40, 50, _
60, 70, 80, 90, 100, _
110, 120, 130, 140, 150, _
160, 170, 180, 190, 200} 'A BUFFER OF SOME DATA TO PASS IN (optional)
'SEND THE RESULTS OF THE PING TO A TEXT FILE
Const File As String = "PCMini_Ping_Results.txt"
Dim lineContent As String
'ADD A BUNCH OF THINGS TO PING
myPingList.Add("172.30.255.16")
myPingList.Add("192.168.104.18")
'...and a bunch more IP addresses....
'LOOP THE STRING LIST OF PLACES WE WANT TO PING
For Each ItemToPing As String In myPingList
'CREATE A NEW PING OBJECT
myPing = New Ping
'ADD EVENT HANDLER FOR THE PING COMPLETED EVENT
'PingResult IS THE SUB ROUTINE BELOW
AddHandler myPing.PingCompleted, _
AddressOf PingResult
'TELL USER WHAT IS BEING PINGED
lineContent = "Sending Async Request To: " & ItemToPing & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
'SEND ASYNC PING REQUEST
myPing.SendAsync(ItemToPing, timeout, buffer, ItemToPing)
Next
'WAIT FOR A KEYPRESS TO CLOSE CONSOLE WINDOW
'oWrite.WriteLine("")
'oWrite.WriteLine("Press any key to close")
'oWrite.ReadKey(True)
End Sub
'THE PING RESULT THAT WILL FIRE WHEN A PING COMPLETES
Private Sub PingResult(ByVal sender As Object, ByVal e As System.Net.NetworkInformation.PingCompletedEventArgs)
Const File As String = "PCMini_Ping_Results.txt"
Dim lineContent As String
'OUTPUT TO USER, e.UserState IS WHAT WE PASSED IN AS THE UserToken (ItemToPing) IN THE ABOVE CODE
'oWrite.WriteLine("")
'oWrite.WriteLine("***********************************")
lineContent = "Result is back from: " & e.UserState.ToString & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
'IF THERE WAS AN ERROR THEN OUTPUT INFORMATION ABOUT THE ERROR
'OTHERWISE OUTPUT INFORMATION ABOUT THE RESULT OF THE PING REQUEST
'THE EVENTARGS e HAVE A LOT OF INFORMATION IN THE e.Reply VARIABLE
If e.Error IsNot Nothing Then
lineContent = "Error: " & e.Error.Message & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
If e.Error.InnerException IsNot Nothing Then
lineContent = "More Info: " & e.Error.InnerException.Message & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
End If
Else
lineContent = "Status: " & e.Reply.Status.ToString & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
lineContent = "Round Trip Time: " & e.Reply.RoundtripTime.ToString & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
lineContent = "Response Buffer Length:" & e.Reply.Buffer.Length.ToString & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
End If
lineContent = "***********************************" & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
lineContent = "" & vbNewLine
My.Computer.FileSystem.WriteAllText(File, lineContent, True)
'THE SENDER IN THE ARGUMENTS IS THE PING OBJECT THAT WE CREATED
'SO NOW REMOVE THE EVENT HANDLER LISTENER AS IT IS NO LONGER NEEDED
'AND CALL Dispose() OF THE PING CLASS TO CLEAN UP UNMANAGED RESOURCES
With DirectCast(sender, Ping)
RemoveHandler .PingCompleted, AddressOf PingResult
.Dispose()
End With
End Sub
End Module
-
Jul 16th, 2010, 10:47 AM
#7
Re: Async Ping How To
really a nice article which i was searching for
but i unable to understand one thing
if the internet / LAN connection it self is disconnected for any reason is there any code to establish that the network was disconnected & resumed at so & so times .
for example
new work disconnected at 10:00 and reconnected at 11:00 hrs
i am trying to use this kind of application.
-
Jul 16th, 2010, 11:58 AM
#8
Re: Async Ping How To
If it is a winforms project, look at the application events section (my project -> application tab -> view application events button)
In there you can select MyApplication Events from the left dropdown and NetworkAvailabilityChanged from the right drop down. This event will fire in the application in the event network availability has changed. The EventArgs passed in has a .IsAvailable boolean property that will indicate if the change was bringing the connection back online or if it just went offline.
-
Jul 16th, 2010, 08:19 PM
#9
Re: Async Ping How To
-
Jul 16th, 2010, 10:07 PM
#10
Re: Async Ping How To
but this event is not triggering until i physically disconnect the LAN/WAN cable from the hub or from the CPU
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|