Visual Basic Internet Routines

Pure VB: Obtaining a Listing of WHOIS Servers
Posted:   Tuesday March 19, 2002
Updated:   Monday December 26, 2011
Applies to:   VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch, MSDN


gethostbyaddr: Obtain Host Name from IP Address
gethostbyname: Resolve Host Name to IP Address
IcmpSendEcho: Ping a Machine by IP Address
IcmpSendEcho: Ping a Machine by Host Name

Pure VB: Obtaining a Listing of WHOIS Servers
Pure VB: Retrieving WHOIS Data for a Registered Domain
Network or DUN connection, Winsock control.

In order to connect a host to the Internet, you must register a domain name with the InterNIC. The InterNIC is responsible for registering top second level domains EDU, ORG, GOV, COM, and NET.

WHOIS is a service that provides information on these second-level domains including contact email addresses, postal addresses and telephone numbers of those who have registered the domain. WHOIS can also help determine whether a domain name is already in use which can be helpful for new site administrators.

The information contained in the InterNIC database can be especially valuable if there is a problem with internetworking between two domains. For example, WHOIS can help determine who to contact if there is a problem with a domain that can not be resolved via email by providing the name and phone number of the administrator at a destination host.

This page shows how, using the VB Winsock control, you can connect to a WHOIS server to request a list of WHOIS servers. The data returned is parsed to extract the server name and country of the server, then used to populate a listbox reflecting the servers for the country selected in the combo.

Provided for additional information is the following listing of common ports:

Well-Known Ports

Port No. Protocol ServiceName



7 TCP echo   Echo
7 UDP echo   Echo
9 TCP discard sink null Discard
9 UDP discard sink null Discard
13 TCP daytime   Daytime
13 UDP daytime   Daytime
17 TCP qotd quote Quote of the day
17 UDP qotd quote Quote of the day
19 TCP chargen ttytst source Character generator
19 UDP chargen ttytst source Character generator
20 TCP ftp-data   File Transfer
21 TCP ftp   FTP Control
23 TCP telnet   Telnet
25 TCP smtp mail Simple Mail Transfer
37 TCP time   Time
37 UDP time   Time
39 UDP rlp resource Resource Location Protocol
42 TCP nameserver name Host Name Server
42 UDP nameserver name Host Name Server
43 TCP nicname whois Who Is
53 TCP domain   Domain Name
53 UDP domain   Domain Name Server
67 UDP bootps dhcps Bootstrap Protocol Server
68 UDP bootpc dhcpc Bootstrap Protocol Client
69 UDP tftp   Trivial File Transfer
70 TCP gopher   Gopher
79 TCP finger   Finger
80 TCP http www, http World Wide Web
88 TCP kerberos krb5 Kerberos
88 UDP kerberos krb5 Kerberos
101 TCP hostname hostnames NIC Host Name Server
102 TCP iso-tsap   ISO-TSAP Class 0
107 TCP rtelnet   Remote Telnet Service
109 TCP pop2 postoffice Post Office Protocol - Version 2
110 TCP pop3 postoffice Post Office Protocol - Version 3
111 TCP sunrpc rpcbind portmap SUN Remote Procedure Call
111 UDP sunrpc rpcbind portmap SUN Remote Procedure Call
113 TCP auth ident tap Authentication Sevice
117 TCP uucp-path   UUCP Path Service
119 TCP nntp usenet Network News Transfer Protocol
123 UDP ntp   Network Time Protocol
135 TCP epmap loc-srv DCE endpoint resolution
135 UDP epmap loc-srv DCE endpoint resolution
137 TCP netbios-ns nbname NETBIOS Name Service
137 UDP netbios-ns nbname NETBIOS Name Service
138 UDP netbios-dgm nbdatagram NETBIOS Datagram Service
139 TCP netbios-ssn nbsession NETBIOS Session Service
143 TCP imap imap4 Internet Message Access Protocol
158 TCP pcmail-srv repository PC Mail Server
161 UDP snmp snmp SNMP
162 UDP snmptrap snmp-trap SNMP TRAP
170 TCP print-srv   Network PostScript
179 TCP bgp   Border Gateway Protocol
194 TCP irc   Internet Relay Chat Protocol
213 UDP ipx   IPX over IP
389 TCP ldap   Lightweight Directory Access Protocol
443 TCP https MCom  
443 UDP https MCom  
445 TCP     Microsoft CIFS
445 UDP     Microsoft CIFS
464 TCP kpasswd   Kerberos (v5)
464 UDP kpasswd   Kerberos (v5)
500 UDP isakmp ike Internet Key Exchange (IPSec)
512 TCP exec   Remote Process Execution
512 UDP biff comsat Notifies users of new mail
513 TCP login   Remote Login
513 UDP who whod Database of who's logged on, average load
514 TCP cmd shell Automatic Authentication
514 UDP syslog    
515 TCP printer spooler Listens for incoming connections
517 UDP talk   Establishes TCP Connection
518 UDP ntalk    
520 TCP efs   Extended File Name Server
520 UDP router router routed RIPv.1, RIPv.2
525 UDP timed timeserver Timeserver
526 TCP tempo newdate Newdate
530 TCP,UDP courier rpc RPC
531 TCP conference chat IRC Chat
532 TCP netnews readnews Readnews
533 UDP netwall   For emergency broadcasts
540 TCP uucp uucpd Uucpd
543 TCP klogin   Kerberos login
544 TCP kshell krcmd Kerberos remote shell
550 UDP new-rwho new-who New-who
556 TCP remotefs rfs rfs_server Rfs Server
560 UDP rmonitor rmonitord Rmonitor
561 UDP monitor    
636 TCP ldaps sldap LDAP over TLS/SSL
749 TCP kerberos-adm   Kerberos administration
749 UDP kerberos-adm   Kerberos administration



 BAS Module Code

 Form Code
To a form add a command button (Command1), a combo (Combo1), a listbox (List1), and a label (Label1).  Add a Winsock control (Winsock1) along with the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private sWinsockCommand As String  'command string since Winsock can't accept parameters
Private sDataIn As String          'in: data blocks received by Winsock
Private sDataBuff As String        'buffer of all data blocks

Private Const LB_SETTABSTOPS As Long = &H192  
Private Const CB_FINDSTRING As Long = &H14C
Private Const CB_FINDSTRINGEXACT As Long = &H158

Private Type WhoisServers
   country  As String
   server   As String
End Type

Private whois() As WhoisServers  'UDT holding the servers and countries

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Private Sub Form_Load()

   ReDim TabArray(0 To 0) As Long
   TabArray(0) = 38
  'set the list tabstops
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 1&, TabArray(0))
   Command1.Caption = "Get Whois Servers"

End Sub

Private Sub Command1_Click()

   If GetWhosisServerList() Then
      Label1.Caption = "Retrieving..."
   End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
   If Winsock1.State = sckConnected Then
   End If
End Sub

Private Function FindStringInCombo(ByVal sItem As String) As Boolean

   'if the combo already contains
   'the string specified as sItem,
   'return True.
    FindStringInCombo = SendMessage(Combo1.hwnd, _
                                    CB_FINDSTRINGEXACT, _
                                    -1, _
                                    ByVal sItem) <> -1

End Function

Private Function GetWhosisServerList() As Boolean

  'the only command required to
  'retrieve the server list is
  'to send "whois-servers"
   sWinsockCommand = "whois-servers" & vbCrLf
   With Winsock1
      .LocalPort = 0
     'avoid error by assuring the
     'sockets are closed.
     'connect to a whois data sever
     'using port 43, defined
     'in Windows' Services file as:
     '  nicname  43/tcp  whois
      .Connect "", 43
      GetWhosisServerList = .State = sckResolvingHost
   End With

End Function

Private Sub Combo1_Click()

   Dim cnt As Long
   If Combo1.ListIndex > -1 Then
      For cnt = 0 To UBound(whois) - 1
        'if "All Servers", just list them
         If Combo1.ListIndex = 0 Then
            List1.AddItem whois(cnt).country & _
                          vbTab & whois(cnt).server
           'if the .country string 
           'matches the selected list 
           'item, add it to the list
            If whois(cnt).country = Combo1.List(Combo1.ListIndex) Then
               List1.AddItem whois(cnt).country & _
                             vbTab & whois(cnt).server
            End If  'If whois(cnt).country
         End If  'If Combo1.ListIndex
      Next cnt

   End If  'If Combo1.ListIndex
End Sub

Private Sub Winsock1_Connect()

  'if a connection was made,
  'pass the retrive command
  'to the server
   If Winsock1.State = sckConnected Then
      Winsock1.SendData sWinsockCommand
   End If
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

   Winsock1.GetData sDataIn
   sDataBuff = sDataBuff & sDataIn

End Sub

Private Sub Winsock1_Close()

   Dim buff()  As String  'buffer for Split()
   Dim country As String  'the country
   Dim pos     As String  'instr() counter
   Dim x       As Long    'array counter
   Dim cnt     As Long    'item counter
   If Winsock1.State = sckClosing Then
     'assure closed

      Combo1.AddItem "All servers"
     'initialize the array to 500 members
      ReDim whois(0 To 500) As WhoisServers
      cnt = 0
     'Split out the returned info using
     'VB6's Split command. Users of
     'VB4-32 or VB5 will need to write
     'code to replicate the Split method.
     'Note that although the server used
     'in this demo ( returns
     'text data containing the CrLf sequence
     'used in Split() below, some servers
     'return data in Unix-format, thereby
     'requiring a set of Replace() calls to
     'change the vbLf in the Unix files
     'to vbCrLf's.
      buff = Split(sDataBuff, vbCrLf)
      For x = 0 To UBound(buff) - 1
        'the server data returned is
        'in the format:
        '    Alabanza, Inc C=US
        'where the first portion is
        'the server, followed by the
        'name of the provider, and the
        'country. Therefore to populate
        'the whois array, the constituent
        'members need to be stripped out.
         If Left$(buff(x), 1) <> ";" And _
            InStr(buff(x), ".") Then
           'locate the start of the country
            pos = InStr(buff(x), "C=")
           'and if found, extract the country code
            If pos Then
               country = Mid$(buff(x), pos + 2, Len(buff(x)))
            End If

           'if the array will exceed 500 items,
           'add more
            If (cnt + 1) Mod 500 = 0 Then
               ReDim Preserve whois(0 To cnt * 2) As WhoisServers
            End If
           'Combo1 lists the countries to
           'display of servers for specific
           'countries, if desired. To prevent
           'multiple entries call a method
           'that determines if the combo
           'currently contains the country,
           'and only add it if it is not
           'already in the combo
            If FindStringInCombo(country) = False Then
               Combo1.AddItem country
            End If
           'add the country and server
           'data to the whois type
            With whois(cnt)
               .country = country
               .server = Left$(buff(x), InStr(buff(x), Chr$(32)))
            End With
           'increment counter
            cnt = cnt + 1
         End If
      Next x
     'since the whois array will contain
     'more members than actually used,
     'trim off unused elements from the array
      ReDim Preserve whois(0 To cnt) As WhoisServers
   End If
  'cause the combo click event
  'to fire to load the listbox
  'and update the label
   Combo1.ListIndex = 0
   Label1.Caption = "Countries Available:"

End Sub


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter