1
'---------------------------------------------------------------------
2 ' This file is part of the Microsoft .NET Framework SDK Code Samples.
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
6 'This source code is intended only as a supplement to Microsoft
7 'Development Tools and/or on-line documentation. See these other
8 'materials for detailed information regarding Microsoft code samples.
10 'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 '---------------------------------------------------------------------
16 Imports System
.Net
.NetworkInformation
17 Imports System
.Globalization
19 Public Class PingClientForm
20 Dim WithEvents pingClient
As New Ping()
22 Private Sub pingClient_PingCompleted( _
23 ByVal sender
As Object, ByVal e
As PingCompletedEventArgs
) _
24 Handles pingClient
.PingCompleted
25 ' Check to see if an error occurred. If no error, then display the
26 ' address used and the ping time in milliseconds
27 If e
.Error Is
Nothing Then
29 pingDetailsTextBox
.Text
&= _
30 "Ping cancelled." & Environment
.NewLine
32 If e
.Reply
.Status
= IPStatus
.Success
Then
33 pingDetailsTextBox
.Text
&= _
34 " " & e
.Reply
.Address
.ToString() & " " & _
35 e
.Reply
.RoundtripTime
.ToString( _
36 NumberFormatInfo
.CurrentInfo
) & "ms " & _
39 pingDetailsTextBox
.Text
&= _
40 " " & GetStatusString(e
.Reply
.Status
) & _
45 ' Otherwise display the error
46 pingDetailsTextBox
.Text
&= _
47 " Ping error." & Environment
.NewLine
49 "An error occurred while sending this ping. " & _
50 e
.Error.InnerException
.Message
.ToString())
52 sendPingButton
.Enabled
= True
55 Private Function GetStatusString(ByVal status
As IPStatus
) As String
59 Case IPStatus
.DestinationHostUnreachable
60 Return "Destination host unreachable."
61 Case IPStatus
.DestinationNetworkUnreachable
62 Return "Destination network unreachable."
63 Case IPStatus
.DestinationPortUnreachable
64 Return "Destination port unreachable."
65 Case IPStatus
.DestinationProtocolUnreachable
66 Return "Destination protocol unreachable."
67 Case IPStatus
.PacketTooBig
68 Return "Packet too big."
69 Case IPStatus
.TtlExpired
71 Case IPStatus
.ParameterProblem
72 Return "Parameter problem."
73 Case IPStatus
.SourceQuench
74 Return "Source quench."
75 Case IPStatus
.TimedOut
83 Private Sub sendPingButton_Click( _
84 ByVal sender
As System
.Object, ByVal e
As System
.EventArgs
) Handles sendPingButton
.Click
86 ' Select all the text in the address box
87 addressTextBox
.SelectAll()
89 If addressTextBox
.Text
.Length
<> 0 Then
90 ' Disable the Send button.
91 sendPingButton
.Enabled
= False
93 pingDetailsTextBox
.Text
&= _
94 "Pinging " & addressTextBox
.Text
& _
95 " . . ." & Environment
.NewLine
98 pingClient
.SendAsync(addressTextBox
.Text
, Nothing)
100 MessageBox
.Show("Please enter an IP address or host name.")
105 Private Sub cancelPingButton_Click( _
106 ByVal sender
As System
.Object, ByVal e
As System
.EventArgs
) Handles cancelPingButton
.Click
108 ' Cancel any pending pings.
109 pingClient
.SendAsyncCancel()