Visual Basic Code of the Week (COTW)
Issue #115
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Pinging another host or IP Address

Future of ASP

The Future of ASP to be Revealed at ASP Connections. Mark Anders from the Microsoft ASP Team will present to ASP Connections conference attendees a keynote talk entitled "The Future of ASP". In this special, exclusive presentation by the creators of ASP, Microsoft will present a comprehensive roadmap of the advances it is making in its ASP technology including server-side UI controls, improved state services, and richer language support. ASP Connections will be held April 30 through May 4, 2000 in Phoenix, Arizona. If you are interested visit our site at

Earn REAL money surfing the web!

If you would like to make some extra cash for surfing the web, jump to


In this Issue

In this issue we discuss how to ping a hostname or an IP address.

If you have any questions about using this module, let us know at


This class module provides a simple way to ping another host. If you use this function on the Internet it will figure out the response time of a domain name. This can be useful for debugging network problems and determining the response of various network hosts around the network. One way we use it is to check the response time to particular web servers on the internet. Not all web servers support the responding to ICMP requests (ping).


Public Property Let PacketSize(lPacketSize As Long)
Public Property Get PacketSize() As Long

The size of the packet used when sent to the host during the Ping process.

Public Property Let PacketCount(lPacketCount As Long)
Public Property Get PacketCount() As Long

The number of packets to send sequentially during the Ping process. To get the status of each ping step, look at the PingStatus event described below.


Public Sub Initialize()

This method needs to be called before the any other routines in this class module. It is the one that initializes the winsock library.

Public Sub Ping(sHostName As String)

The Ping method uses the ICMPSendEcho function to perform the ping operation. Microsoft describes the ICMPSendEcho command as follows: The ICMPSendEcho() function sends an ICMP echo request to the specified destination IP address and returns any replies received within the timeout specified. The API is synchronous, requiring the process to spawn a thread before calling the API to avoid blocking (which we did not implement for this version in Visual Basic). An open IcmpHandle is required for the request to complete. IcmpCreateFile() and IcmpCloseHandle() functions are used to create and destroy the context handle.


Public Event PingStatus(lStatus As Long, sResultString As String, sRespondingHost As String, iBytesSent As Integer, lRoundTripTime As Long, lTimeToLive As Byte)

This event will be fired for each ICMPSendEcho call (ping call). lStatus is the return code. The valid return codes can be found near the top of the cICMP class. A return code of 0 means success.

sResultString is an English version of the error message. If it is successful the text "Success" is returned.

sRespondingHost is the IP address of the machine that responded to the ping request.

iBytesSent is usually the value defined in PacketSize.

lRoundTripTime is the number of milliseconds it took to respond to the ping request. This is the value that is most useful to determine how fast or slow your connection is.

lTimeToLive has to do with the number of routers a packet passes through. We could not find a good definition. If anyone has a good definition for Time To Live please forward it to us at

Sample Usage

The below sample shows will ping domain name or host name specified in Text1.Text. This example assumes you have created a form and added a textbox called Text1, a listbox called List1 and a command button called Command1. Using WithEvents when creating an object allows the class to return information to the calling object or form. This is how the status information for each ping request is returned to the form.

Option Explicit

Private WithEvents icmp As cICMP

Private Sub Command1_Click()
    Set icmp = New cICMP
    icmp.PacketCount = 5
    icmp.PacketSize = 32
    icmp.Ping Text1.Text
    Set icmp = Nothing
End Sub

Private Sub Form_Load()

End Sub

Private Sub icmp_PingStatus(lStatus As Long, sResultString As String, sRespondingHost As String, lBytesSent As Integer, lRoundTripTime As Long, lTimeToLive As Byte)
    If lStatus = 0 Then
        List1.AddItem "Reply from " & sRespondingHost & ": Bytes = " & _
                    Trim$(CStr(lBytesSent)) & " RTT = " & Trim$(CStr(lRoundTripTime)) & _
                     "ms TTL = " & Trim$(CStr(lTimeToLive))
        List1.AddItem "Error " & lStatus & " - " & sResultString
    End If
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945