Section 6.2. Ping Another Computer

6.2. Ping Another Computer

The Internet is a dynamic network where computers appear and drop out of sight without warning. One simple test an application can always perform to check if a computer is reachable is to send a ping message. Technically, a ping is the equivalent of asking another computer, "Are you there?" To get its answer, ping sends a special type of message over a low-level Internet protocol called ICMP (Internet Control Message Protocol).

Note: Need to find out if a computer is reachable over the Internet? With the new Ping class, you can make this simple request without a tangle of low-level socket code.

Sending a ping message using the classes found in the System.Net namespaces is challenging and requires dozens of low-level code statements that deal with raw sockets. In .NET 2.0, there's a much simpler solution with the new Ping class in the System.Net.NetworkInformation namespace.

6.2.1. How do I do that?

To ping a computer, you use the Ping( ) method of the My.Computer.Network object. This approach gives you convenient access to the bare minimum ping functionality. The Ping( ) method returns true or False depending on whether it received a response from the computer you're trying to contact.

Note: Windows includes a utility called ping.exe that you can use to ping other computers at the command line.

Example 6-3 uses this method in order to contact the web server at

Example 6-3. Pinging a remote computer
Module PingTest          Sub Main( )         Dim Success As Boolean              ' Try to contact (wait 1000 milliseconds at most,         ' which is the default if you don't specify a timeout).         Success = My.Computer.Network.Ping("", 1000)         Console.WriteLine("Did the computer respond? " & Success)     End Sub      End Module

When you call Ping( ), you specify two parameters: the URL or IP address for the computer you're trying to reach (e.g., or and, optionally, a maximum wait time in milliseconds. Once this limit is reached, the request times out, and the Ping( ) method returns False to indicate the failure.

Warning: A ping message is a low-level test that doesn't necessarily correspond to the availability of services on a particular computer. For example, even if you can ping, that doesn't mean that its search engine web pages are available and working properly. Similarly, web servers or firewalls often reject ping messages to restrict the possibility of someone launching a denial of service attack by flooding the computer with millions of spurious requests. For that reason, if you ping, you won't receive a response, even though you can still surf to their web site using that address.

6.2.2. What about...

...getting more information from the remote computer? The My.Computer.Network object doesn't return any additional information about the results of the ping test. For example, you won't find out how long it took to receive a response, which is a key statistic used by some applications, such as peer-to-peer software, to rank the connection speed of different computers.

To get more information, you need to head directly to the Ping class in the System.Net.NetworkInformation namespace. It returns a PingResult object with several pieces of information, including the time taken for a response. The following code snippet puts this approach to the test. It assumes that you've imported the System.Net.NetworkInformation namespace:

Dim Pinger As New Ping Dim Reply As PingReply = Pinger.Send("") Console.WriteLine("Time (milliseconds): " & Reply.RoundTripTime) Console.WriteLine("Exact status: " & Reply.Status.ToString( )) Console.WriteLine("Adress contacted: " & Reply.Address.ToString( ))

Here's some sample output:

Time (milliseconds): 61 Exact status: Success Adress contacted:

The Ping class also provides a SendAsync( ) method you can use to ping a computer without stalling your code (you can handle the response in another thread when a callback fires), and other overloaded versions of the Send( ) method that allow you to set low-level options (like the number of hops the ping message will travel before expiring).

6.2.3. Where can I learn more?

To use this added networking muscle, read up on the Ping class in the MSDN Help.

Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123 © 2008-2017.
If you may any questions please contact us: