IP to Country component for ColdFusion 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.

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 ColdFusion document

Create a new blank webdocument with the ".cfm" extention. We have first have to create the form to enter the IP or hostname and display the results.

Step 3: Create an instance of the ActiveSocket object

Before we can use the ActiveSocket toolkit to perform lookups, we have to create an instance of the IPtoCountry object.

Use the following ColdFusion code to do this:

 <cfobject class="ActiveXperts.Tcp" type="com" name="objSocket" Action="Create"> 

Step 4: Perform an IP to country lookup

After creation of the IPtoCountry object, we can call its functions to perform an IP to country lookup. This can be done by just three lines of code, setting the hostname or IP address, perform the lookup and read the result.

Below you can find the sourcecode to create a simple online IP to country lookup utility:

<!--- Creating the activexperts IPtoCountry object --->
<cfobject class="ActiveXperts.IPtoCountry" type="com" name="objSocket" Action="Create">

<!--- get the location of the IP address --->
<cfscript>

  if(IsDefined("URL.Submitbutton")){

    // Get the hostname and strip some the prefix
    strHostname = URL.txtHostname;
    strHostname = Replace(strHostname, "http://", "");
    strHostname = Replace(strHostname, "https://", "");
    strHostname = Replace(strHostname, "ftp://", "");
    // And so on..

    // First query the hostname/ip-address
    objSocket.Clear();
  	objSocket.Logfile = "C:\IPToCountry_Log.txt";
  	objSocket.Host = URL.txtHostname;
	  objSocket.Query();

    // If the query has succesfully executed, get
    // the results
    if(objSocket.LastError eq 0)
    {
      strCountry = objSocket.CountryName;
      strCountryCode = objSocket.CountryCode;
    }
    else{
      strCountry = "unknown...";
      strCountryCode = "unknown...";		
    }
	
    // The results:
    strLastError = objSocket.LastError & " : " & objSocket.GetErrorDescription(objSocket.LastError);
	
  }
  else{

    // Fill in the default values of the form:
    strHostname = "forum.activexperts.com";
    strCountry = "";
    strCountryCode = "";
    strResult = "";
    strLastError = "";

  }
</cfscript>


<cfoutput>

<html>

<head>
  <title>ActiveSocket IP to Country Sample</title>
  <style>
    input{
      font-family: verdana;
      font-size: x-small;
      border: 1px solid black;
      width: 250px;
    }
    table{
      font-family: verdana;
      font-size: x-small;
      border: 1px solid black;
      width: 400px;
    }
  </style>
</head>

<body>

<form>
  <div align=center>
    <h2><font face=verdana>ActiveSocket IP to Country ColdFusion Sample</font></h2>
    <table>
      <tr>
        <td width=150>Hostname:</td>
        <td><input type="text" name="txtHostname" value="#strHostname#"></td>
      </tr>
      <tr>
        <td width=150>Country:</td>
        <td><input type="text" name="txtCountry" value="#strCountry#" disabled="true"></td>
      </tr>
      <tr>
        <td width=150>Country code:</td>
        <td><input type="text" name="txtCountryCode" value="#strCountryCode#" disabled="true"></td>
      </tr>
      <tr>
        <td width=150>Result:</td>
        <td><input type="text" name="txtResult" value="#strLastError#" disabled="true"></td>
      </tr>
      <tr>
        <td width=150> </td>
        <td><input type="submit" name="Submitbutton" value="Get country!" style="background-color: red; color: white;"></td>
      </tr>
    </table>
  </div>
</form>
</body>
</html>
</cfoutput>

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.