My.Computer.Network.Ping Method
Pings the specified server.
' Usage
Dim value As Boolean = My.Computer.Network.Ping(hostNameOrAddress)
Dim value As Boolean = My.Computer.Network.Ping(address)
Dim value As Boolean = My.Computer.Network.Ping(hostNameOrAddress ,timeout)
Dim value As Boolean = My.Computer.Network.Ping(address ,timeout)
' Declaration
Public Function Ping( _
ByVal hostNameOrAddress As String _
) As Boolean
' -or-
Public Function Ping( _
ByVal address As System.Uri _
) As Boolean
' -or-
Public Function Ping( _
ByVal hostNameOrAddress As String, _
ByVal timeout As Integer _
) As Boolean
' -or-
Public Function Ping( _
ByVal address As System.Uri, _
ByVal timeout As Integer _
) As Boolean
Parameters
hostNameOrAddress
String. The URL, computer name, or IP number of the server to ping. Required.address
Uri. The URI of the server to ping. Required.timeout
Int32. Time threshold in milliseconds for contacting the destination. Default is 500. Required.
Return Value
Boolean indicating whether or not the operation was successful.
Exceptions
The following conditions may cause an exception to be thrown:
No network connection is available (InvalidOperationException).
URL was not valid (PingException).
Remarks
The Ping method is not a fail-safe method for determining the availability of a remote computer: the ping port on the target computer may be turned off or the ping request may be blocked by a firewall or router.
The address passed to the Ping method must be DNS resolvable and cannot be preceded by "http://".
Tasks
The following table lists examples of tasks involving the My.Computer.Network.Ping method.
To |
See |
---|---|
Determine whether a remote computer or host is available |
How to: Determine if a Remote Computer is Available in Visual Basic |
Example
This example reports whether or not the server can be pinged by determining whether the Ping method returns True.
If My.Computer.Network.Ping("198.01.01.01") Then
MsgBox("Server pinged successfully.")
Else
MsgBox("Ping request timed out.")
End If
Replace "198.01.01.01" with the IP address, URL, or computer name of the server to ping.
This example reports whether or not the server can be pinged by determining whether the Ping method returned True and specifies a timeout interval of 1000 milliseconds.
If My.Computer.Network.Ping("www.cohowinery.com",1000) Then
MsgBox("Server pinged successfully.")
Else
MsgBox("Ping request timed out.")
End If
Replace "www.cohowinery.com" with the IP address, URL, or computer name of the server to ping.
Requirements
Namespace:Microsoft.VisualBasic.Devices
Class:Network
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Availability by Project Type
Project type |
Available |
---|---|
Windows Application |
Yes |
Class Library |
Yes |
Console Application |
Yes |
Windows Control Library |
Yes |
Web Control Library |
Yes |
Windows Service |
Yes |
Web Site |
Yes |
Permissions
The following permissions may be necessary:
Permission |
Description |
---|---|
Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
|
Describes a set of security permissions applied to code. Associated enumeration: ControlPrincipal. |
|
Controls rights to access HTTP Internet resources. Associated enumeration: Unrestricted. |
|
Controls rights to make or accept connections on a transport address. Associated enumeration: Unrestricted. |
|
Controls access to a Windows NT performance counter component. Associated enumeration: Unrestricted. |
|
Controls access to network information and traffic statistics for the local computer. Associated enumeration: Ping. |
For more information, see Code Access Security and Requesting Permissions.