Ping.SendAsynch method

delstar

Well-known member
Joined
Jun 9, 2006
Messages
59
Programming Experience
1-3
A friend of mine and I are writing a network monitoring app in VB.NET. Right now, for scanning the subnet, it steps through and pings each address individually using the ping.send method. We want to speed it up using Ping.SendAsync, but have no idea as to how to use this. I can find any sample code in VB for it, and MSDN's explanation was no help. So, the question is : does anyone know of a sample we can look at or could give me the barebones of an example?
 
straight from the msdn2 online documentation

Found this at: http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

I know it's C#, but you can get the idea
VB.NET:
[/COLOR][/FONT]using[/COLOR] System;
[COLOR=blue]using[/COLOR] System.Text;
[COLOR=blue]using[/COLOR] System.Net;
[COLOR=blue]using[/COLOR] System.Net.NetworkInformation;
[COLOR=blue]using[/COLOR] System.ComponentModel;
[COLOR=blue]using[/COLOR] System.Threading;
 
[COLOR=blue]namespace[/COLOR] Examples.System.Net.NetworkInformation.PingTest
{
[COLOR=blue]public[/COLOR] [COLOR=blue]class[/COLOR] PingExample
{
[COLOR=blue]public[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] Main ([COLOR=blue]string[/COLOR][] args)
{
[COLOR=blue]if[/COLOR] (args.Length == 0)
throw [COLOR=blue]new[/COLOR] ArgumentException ("Ping needs a host or IP Address.");
 
[COLOR=blue]string[/COLOR] who = args[0];
AutoResetEvent waiter = [COLOR=blue]new[/COLOR] AutoResetEvent ([COLOR=blue]false[/COLOR]);
 
Ping pingSender = [COLOR=blue]new[/COLOR] Ping ();
 
[COLOR=green]// When the PingCompleted event is raised,[/COLOR]
[COLOR=green]// the PingCompletedCallback method is called.[/COLOR]
pingSender.PingCompleted += [COLOR=blue]new[/COLOR] PingCompletedEventHandler (PingCompletedCallback);
 
[COLOR=green]// Create a buffer of 32 bytes of data to be transmitted.[/COLOR]
[COLOR=blue]string[/COLOR] data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
 
[COLOR=green]// Wait 12 seconds for a reply.[/COLOR]
[COLOR=blue]int[/COLOR] timeout = 12000;
 
[COLOR=green]// Set options for transmission:[/COLOR]
[COLOR=green]// The data can go through 64 gateways or routers[/COLOR]
[COLOR=green]// before it is destroyed, and the data packet[/COLOR]
[COLOR=green]// cannot be fragmented.[/COLOR]
PingOptions options = [COLOR=blue]new[/COLOR] PingOptions (64, [COLOR=blue]true[/COLOR]);
 
Console.WriteLine ("Time to live: {0}", options.Ttl);
Console.WriteLine ("Don't fragment: {0}", options.DontFragment);
 
[COLOR=green]// Send the ping asynchronously.[/COLOR]
[COLOR=green]// Use the waiter as the user token.[/COLOR]
[COLOR=green]// When the callback completes, it can wake up this thread.[/COLOR]
pingSender.SendAsync(who, timeout, buffer, options, waiter);
 
[COLOR=green]// Prevent this example application from ending.[/COLOR]
[COLOR=green]// A real application should do something useful[/COLOR]
[COLOR=green]// when possible.[/COLOR]
waiter.WaitOne ();
Console.WriteLine ("Ping example completed.");
}
 
[COLOR=blue]private[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
[COLOR=green]// If the operation was canceled, display a message to the user.[/COLOR]
[COLOR=blue]if[/COLOR] (e.Cancelled)
{
Console.WriteLine ("Ping canceled.");
 
[COLOR=green]// Let the main thread resume. [/COLOR]
[COLOR=green]// UserToken is the AutoResetEvent object that the main thread [/COLOR]
[COLOR=green]// is waiting for.[/COLOR]
((AutoResetEvent)e.UserState).Set ();
}
 
[COLOR=green]// If an error occurred, display the exception to the user.[/COLOR]
[COLOR=blue]if[/COLOR] (e.Error != [COLOR=blue]null[/COLOR])
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ());
 
[COLOR=green]// Let the main thread resume. [/COLOR]
((AutoResetEvent)e.UserState).Set();
}
 
PingReply reply = e.Reply;
 
DisplayReply (reply);
 
[COLOR=green]// Let the main thread resume.[/COLOR]
((AutoResetEvent)e.UserState).Set();
}
 
[COLOR=blue]public[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] DisplayReply (PingReply reply)
{
[COLOR=blue]if[/COLOR] (reply == [COLOR=blue]null[/COLOR])
[COLOR=blue]return[/COLOR];
 
Console.WriteLine ("ping status: {0}", reply.Status);
[COLOR=blue]if[/COLOR] (reply.Status == IPStatus.Success)
{
Console.WriteLine ("Address: {0}", reply.Address.ToString ());
Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
}
}
}
 
Last edited by a moderator:
I ran the above code in a C# to VB.Net converter online http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx
Haven't reviewed the conversion in VS, but it looks nice and easy to read:
VB.NET:
Imports System 
Imports System.Text 
Imports System.Net 
Imports System.Net.NetworkInformation 
Imports System.ComponentModel 
Imports System.Threading 
Namespace Examples.System.Net.NetworkInformation.PingTest 
 
Public Class PingExample 
 
Public Shared Sub Main(ByVal args As String()) 
 If args.Length = 0 Then 
  Throw New ArgumentException("Ping needs a host or IP Address.") 
 End If 
 Dim who As String = args(0) 
 Dim waiter As AutoResetEvent = New AutoResetEvent(False) 
 Dim pingSender As Ping = New Ping 
 AddHandler pingSender.PingCompleted, AddressOf PingCompletedCallback 
 Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 
 Dim buffer As Byte() = Encoding.ASCII.GetBytes(data) 
 Dim timeout As Integer = 12000 
 Dim options As PingOptions = New PingOptions(64, True) 
 Console.WriteLine("Time to live: {0}", options.Ttl) 
 Console.WriteLine("Don't fragment: {0}", options.DontFragment) 
 pingSender.SendAsync(who, timeout, buffer, options, waiter) 
 waiter.WaitOne 
 Console.WriteLine("Ping example completed.") 
End Sub 
 
Private Shared Sub PingCompletedCallback(ByVal sender As Object, ByVal e As PingCompletedEventArgs) 
 If e.Cancelled Then 
  Console.WriteLine("Ping canceled.") 
  CType(e.UserState, AutoResetEvent).Set 
 End If 
 If Not (e.Error Is Nothing) Then 
  Console.WriteLine("Ping failed:") 
  Console.WriteLine(e.Error.ToString) 
  CType(e.UserState, AutoResetEvent).Set 
 End If 
 Dim reply As PingReply = e.Reply 
 DisplayReply(reply) 
 CType(e.UserState, AutoResetEvent).Set 
End Sub 
 
Public Shared Sub DisplayReply(ByVal reply As PingReply) 
 If reply Is Nothing Then 
  Return 
 End If 
 Console.WriteLine("ping status: {0}", reply.Status) 
 If reply.Status = IPStatus.Success Then 
  Console.WriteLine("Address: {0}", reply.Address.ToString) 
  Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime) 
  Console.WriteLine("Time to live: {0}", reply.Options.Ttl) 
  Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment) 
  Console.WriteLine("Buffer size: {0}", reply.Buffer.Length) 
 End If 
End Sub 
End Class 
End Namespace
 
We found a problem in Ping.Asynch. Try putting it in a For...Next loop and see what it spits out at you. Anyone have a clue as what to do with this? Keep in mind that this is for a network scanner, so being able to send multiple pings at once is the primary goal. Right now we're just creating a thread for each one. Seems to be working ok, so far, but in a very unsafe manner.
 
Back
Top