© 2004 Microsoft Corporation. All rights reserved.
Figure 1 Implementing the Dns Class
Imports System
Imports System.Net

Namespace Graymad

   Public Class MyDNS

      Public Shared Function Lookup(ByVal IP As IPAddress) As String()
         Dim MyHost As IPHostEntry
         MyHost = Dns.GetHostByAddress(IP)
         If Not MyHost.Aliases.Length = 0 Then
            Dim Aliases() As String
            Aliases = MyHost.Aliases
            ReDim Preserve Aliases(Aliases.GetUpperBound(0) + 1)
            Aliases(Aliases.GetUpperBound(0)) = MyHost.HostName
            Lookup = Aliases
         Else
            Dim Aliases(1) As String
            Aliases(0) = MyHost.HostName
            Lookup = Aliases
         End If
      End Function

      Public Shared Function Lookup(ByVal Hostname As String) As String()
         Dim MyHost As IPHostEntry
         Dim Addresses() As IPAddress
         Dim Counter As Integer

         MyHost = Dns.GetHostByName(Hostname)
         Addresses = MyHost.AddressList

         Dim AddressString(Addresses.GetUpperBound(0)) As String
         For Counter = 0 To Addresses.GetUpperBound(0)
            AddressString(Counter) = Addresses(Counter).ToString()
         Next
         Lookup = AddressString
      End Function

   End Class
End Namespace
Figure 2 Implementing the TcpClient Class
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets

Namespace Graymad
   Public Class MyWHOIS
      Public Shared Function Lookup(ByVal IP As IPAddress) As String
         Dim Address As String = IP.ToString()
         Return Lookup(Address, True)
      End Function

      Public Shared Function Lookup(ByVal Hostname As String, _
          Optional ByVal ArgIsIP As Boolean = False) As String

         Dim WhoIsClient As New TcpClient()
         Dim WhoIsStream As NetworkStream
         Dim WhoIsWriter As StreamWriter
         Dim WhoIsReader As StreamReader
         Dim ReturnString As String

         Hostname &= vbCrLf
         If ArgIsIP Or Hostname.SubString(0,1) = "!" Then
            WhoIsClient.Connect("whois.arin.net", 43)
         Else
            WhoIsClient.Connect("whois.networksolutions.com", 43)
         End If

         WhoIsStream = WhoIsClient.GetStream()

         WhoIsWriter = New StreamWriter(WhoIsStream)
         WhoIsWriter.Write(Hostname)
         WhoIsWriter.Flush()

         WhoIsReader = New StreamReader(WhoIsStream)
         ReturnString = WhoIsReader.ReadToEnd()

         WhoIsStream.Close()
         WhoIsClient.Close()

         Lookup = ReturnString
      End Function
   End Class
End Namespace
Figure 3 ASP.NET Client for a DNS/WHOIS Component
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="Graymad" %>
<%@ Import Namespace="System.Net" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If IsPostBack() Then
               Dim Host As String = Textbox1.Text
               Dim Addresses() As String
               Dim Counter As Integer
               Dim ResponseText As New TextBox
               ResponseText.TextMode = TextBoxMode.Multiline
               ResponseText.Columns = 80
               ResponseText.Rows = 15
               If RadioButton1.Checked Then 'DNS lookup
                  Try
                     Dim IP As IPAddress = IPAddress.Parse(Host)
                     Addresses = MyDNS.Lookup(IP)
                     For Counter = 0 To Addresses.GetUpperBound(0)
                        ResponseText.Text &= Addresses(Counter) & _
                           vbCrLf
                     Next
                  Catch e As FormatException
                     Addresses = MyDNS.Lookup(Host)
                     For Counter = 0 To Addresses.GetUpperBound(0)
                        ResponseText.Text &= Addresses(Counter) & _
                           vbCrLf
                     Next
                  End Try
               Else 'WHOIS lookup
                  Try
                     Dim IP As IPAddress = IPAddress.Parse(Host)
                     ResponseText.Text &= MyWhoIs.Lookup(IP)
                  Catch e As FormatException
                     ResponseText.Text &= MyWhoIs.Lookup(Host)
                  End Try
               End If
               Page.Controls.Add(ResponseText)
               Table1.Visible = False
            End If
         End Sub
      </script>
   </head>
   <body>
      <form runat="server">
         <table
            id="Table1"
            bordercolor="#0000a0"
            cellspacing="0"
            cellpadding="2"
            border="2"
            runat="server">
            <tbody>
               <tr>
                  <td colspan="3">
                     Please enter a hostname or IP address to look up
                        and choose the type of lookup below:
                  </td>
               </tr>
               <tr>
                  <td>
                     <asp:label id="Label1" 
                        font-bold="True"
                        runat="server">
                        IP Address or Hostname:
                     </asp:label>
                  </td>
                  <td>
                     <asp:textbox id="TextBox1" runat="server"/>
                  </td>
                  <td>
                     <asp:radiobutton id="RadioButton1" 
                        groupname="DNS_WHOIS" 
                        text="DNS" 
                        checked="true" 
                        runat="server"/>
                     <br>
                     <asp:radiobutton id="RadioButton2" 
                        text="WHOIS" 
                        groupname="DNS_WHOIS" 
                        runat="server"/>
                  </td>
               </tr>
               <tr>
                  <td colspan="3">
                     <asp:button id="Button1" 
                        text="Submit"
                        runat="server"/>
                  </td>
               </tr>
            </tbody>
         </table>
      </form>
   </body>
</html>

Home
Topics
Issues
Columns
Downloads
Subscribe
Contact Us

MSDN Magazine