|
|
Code Samples
The product includes numerous VB.NET, C# and ASP.NET sample applications. All
samples include complete source code. For more detailed sample code please
refer to the sample applications.
Pinging a Server
Listing 1 demonstrates using the asynchronous BeginEcho
method to send an ICMP echo message to a server.
The OnEchoCompleted event is fired when the ICMP echo reply
is received or a failure occurs. Alternatively, the synchronous
Echo method may be used.
Private echoClient As EchoClient
Private Sub StartEcho()
' Start the ping.
Try
EchoClient = New EchoClient(textBoxServerAddress.Text)
Catch exception As Exception
' Handle the failure
End Try
EchoClient.SynchronizingObject = Me
AddHandler EchoClient.EchoCompleted, AddressOf OnEchoCompleted
sendDateTime = DateTime.Now
EchoClient.BeginEcho(echoData)
End Sub
Private Sub OnEchoCompleted(ByVal sender As Object, _
ByVal e As EchoCompletedEventArgs)
If EchoClient Is Nothing Then Exit Sub
' Check the results of the ping.
Try
EchoClient.EndEcho(e.AsyncResult)
Dim timeSpan As TimeSpan
timeSpan = DateTime.Now.Subtract(sendDateTime)
Dim stringBuilder As New StringBuilder()
stringBuilder.AppendFormat("Received echo reply after {0} secs", _
timeSpan.TotalSeconds)
Catch exception As Exception
' Handle the failure
End Try
sendDateTime = DateTime.Now
EchoClient.BeginEcho(echoData)
End Sub
|
Listing 1 Pinging a server
|