IP to Country component for ASP IIS for Windows

ActiveSocket is a Network Communication component for Windows Developers. It runs on any 32 bit and 64 bit Windows Platform, incl. Windows 7, Windows 2008, Windows 2003, Windows 2000, Windows Vista and Windows XP.
It features many IP protocols, incl.: SSH (Secure Shell), RSH (Remote Shell), HTTP(s), FTP, ICMP Ping, NTP, SNMP v1/v2c (Get,GetNext,Set), SNMP MIB translation, SNMP Trap Sender, SNMP Trap Receiver, Telnet, DNS, TCP, UDP, IP-to-Country, Wake-On-LAN and more. Samples are included many popular development platforms, incl. Visual C# .NET, Visual Basic .NET, ASP .NET, Visual Basic, Visual C/C++, ASP, Java, Javascript, PHP, Borland Delphi, Borland C++ Builder, ColdFusion.

ActiveSocket can be well integrated into ASP environments. This document describes how ActiveSocket can be integrated into ASP projects.

Step 1: Download and install ActiveSocket

Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Web Site

First, create a new directory on the IIS Server's file system. This directory will hold the ASP later on.

From the 'Start menu', click on 'Administrative Tools' and click on 'Internet Information Services (IIS) Manager'. Right-click on the 'Web Sites' container and choose 'New->Web Site':

(Click on the picture to enlarge)

The 'Web Site Creation Wizard' is shown, guiding you thorugh the process of creating a new web site. Provide all necessary information:

  • Description - a friendly description of the new site;
  • IP / Port / Host Header - choose your preferred way to distinguish between other web sites on the server;
  • Path - select the directory that will store the ASP file(s);
  • Web Site Access Permissions - in the Web Site Access Permissions dialog, enable 'Read' and 'Run scripts (such as ASP)';

You're now able to write an ASP script to use IP protocols with ActiveSocket.

Step 3: Create the ActiveSocket object in ASP

Create a new ASP script called DEFAULT.ASP in the directory that was created in Step2, using your favorite editor. On top of the ASP code, insert the following lines to declare and create the ActiveSocket object:

   <object runat=server progid="ActiveXperts.IPtoCountry" id=objIPtoCountry ></object>

Step 4: Test a small piece of ASP

Now, test if your new web site is working well with ActiveSocket using your browser. If you are using Microsoft Internet Explorer, it is recommended to disable friendly error message because this default setting doesn't show any ASP error message, making it hard to debug if there are any problems:

  • Choose' Internet Options' from the 'Tools' menu;
  • Select the 'Advanced' tab;
  • In the 'Browsing' folder, de-select the 'Show friendly HTTP error messages' option;
  • Click 'OK' to close the dialog.

Now, use the following piece of code in your DEFAULT.ASP page:

   
<html>
  <head>
    <title>ActiveSocket IP To Country ASP Sample</title>
    <style>
     table
     {
       border: 1px solid navy;
       font-family: verdana;
       font-size: x-small;
       color: navy;
     }
  
     input
     {
       border: 1px solid navy;
       background-color: #F9F9F9;
       font-family: verdana;
       font-weight: x-small;
       color: navy;
     }
  
     h2
     {
       font-family: verdana;
       color: navy;
     }
    </style>
  </head>
  <body>

<%
If request("submitbutton") <> "" Then
  Dim strUrl, strLogfile, arrResults

  strLogfile = request    ("strLogfile")
  strUrl     = request    ("strHostname")
  
  arrResults = GetCountry (checkUrl(strUrl), strLogfile)
End If
%>

    <div align=center>
    <form>
    <h2>ActiveSocket IP To Country ASP Sample</h2>
  
    <font size=1 face=verdana color=black><b>Please fill in the form below:</b></font>
    <table>
      <tr>
        <td>The hostname you want to know the location of:</td>
        <td><input type=text size=40 name="strHostname" value="forum.activexperts.com"></td> 
      </tr>
      <tr>
        <td>The file you want to write a log to:</td>
        <td><input type=text size=40 name="strLogfile" value="C:\logfile.txt"></td> 
      </tr>
      <tr>
        <td></td>
        <td><input type=submit name="submitbutton" value="Get country!"></td> 
      </tr>
    </table>
    </form>


  <% If request("submitbutton") <> "" Then %>
	
    <font size=1 face=verdana color=black><b>Results:</b></font>	
    <table width=635>
      <tr>
        <td width=150>Hostname:</td>
        <td><% = request("strHostname") %></td>
      </tr>
      <tr>
        <td width=150>Location:</td>
        <td><% = arrResults(0) & " : " & arrResults(1) %></td>
      </tr>
      <tr>
        <td width=150>Results:</td>
        <td><% = arrResults(2) & " : " & arrResults(3) %></td>
      </tr>
    </table>
  <% End If %>
</div>

</body>
</html>

<%
Function checkUrl(strUrl)
  If strUrl <> "" Then
    checkUrl = replace(strUrl, "http://", "")
  Else
    checkUrl = "error"
  End if    
End Function
%>

<%
Function GetCountry(strHost, strLogfile)
  If strHost <> "error" Then
    Dim objSocket, arrResults(3)

    Set objSocket     = Server.CreateObject("ActiveXperts.IPToCountry")

    objSocket.Logfile = strLogfile
    objSocket.Host    = strHost

    objSocket.Query

    arrResults(0)     = objSocket.CountryCode
    arrResults(1)     = objSocket.CountryName
    arrResults(2)     = objSocket.LastError
    arrResults(3)     = objSocket.GetErrorDescription(objSocket.LastError)

    GetCountry        = arrResults

    Set objSocket     = Nothing
  Else
    GetCountry = ""
  End If
End Function
%>

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/asocket.