ActiveXperts Network Component

Quicklinks

ColdFusion IP to Country Sample Source Code

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.


This document describes how the Network Component FtpServer object can be integrated into your projects.

The most important functions of the FtpServer object are:

  • Connect - connect to the (remote) FTP server on port 21 or any alternate port;
  • Disconnect - to diconnect after a connect call;
  • GetCurrentDir - retrieve the current directory;
  • ChangeDir - change the current directory;
  • CreateDir - create a new directory;
  • RenameDir - rename a directory;
  • DeleteDir - delete a directory;
  • FindFile - find a specific file in the current directory;
  • FindFirstFile - iterate over all files in the current directory; find the first file;
  • FindNextFile - iterate over all files in the current directory; find the next file;
  • RenameFile - rename a file in the current directory;
  • DeleteFile - delete a file in the current directory;
  • GetFile - get (download) a file, either using binary transfer or ASCII transfer;
  • PutFile - put (upload) a file, either using binary transfer or ASCII transfer;

Step 1: Download and install Network Component

Download Network Component 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. First of all we are going to build the form whitch commands and properties of the device can be filled in. Then we are going to make a source code that connects to the device.

Step 3: Create an instance of the Network Component object

Before we can use the Network Component to perform lookups, we have to create an instance of the IPtoCountry object. Use the following ColdFusion code to do this:

 <cfobject class="ax_AxNetwork.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:

Appendix: Full source code

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

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

      // Fill in the default values of the form:
    strHost = "forum.activexperts.com";
    strCountryName = "";
    strCountryCode = "";
    strCountryCodeEx = "";
    strResult = "";
    strLastError = "";
    strLogfile = GetTempDirectory() & "IPToCountry.log";
    strComponent = "Module [" & objSocket.Module & "]; Build [" & objSocket.Build & "]";
    
    if(isDefined("URL.txtHost")){
     strHost = URL.txtHost;
    }
    
    if(isDefined("URL.txtCountryName")){
      strCountryName = URL.txtCountryName;
    }
    
    if(isDefined("URL.txtCountryCode")){
     strHost = URL.txtCountryCode;
    }
    
    if(isDefined("URL.txtCountryCodeEx")){
     strHost = URL.txtCountryCodeEx;
    }
    
    if(isDefined("URL.txtLogFile")){
      strLogFile = URL.txtLogFile;
    }

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

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

    // First query the hostname/ip-address
    objSocket.Clear();
  	objSocket.Logfile = strLogFile;
  	objSocket.Host = URL.txtHost;
	  objSocket.Query();

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


<cfoutput>

<html>

<head>
  <title>ActiveXperts Network Component IP to Country Sample</title>
    <style>
  <!--
  body{
    text-align: center;
  }
  h2{
   font-family: verdana;
   color: navy;
   font-weight: bold;
  }
  form{
   margin: 0px;
  }
  .input{
   font-family: verdana;
   font-size: 9pt;
   color: navy;
   border: 1px solid navy;
   background-color: white;
   width: 325px;
  }  
  .button{
   font-family: verdana;
   font-size: 9pt;   
   width: 325px;
  }
  .table{
   font-family: verdana;
   font-size: 9pt;
   color: navy;
   border: 1px solid navy;
   width: 500px;
  }
  div{
    width: 500; 
    height: 250px; 
    overflow: auto; 
    text-align: left; 
    font-size: xx-small; 
    font-family: verdana;
    border: 1px solid navy;
  }
  a{
    font-family: verdana;
    font-size: 8pt;
    color: red;
  }
  -->
 </style>
</head>

<body>

<form>


	
<h2>ActiveXperts Network Component CF Sample - IP to Country</h2>
  <table class=table>
    <tr>
      <td width=150>Component:</td>
      <td>#strComponent#</td>
    </tr>
    <tr>
      <td width=150>Host:</td>
      <td><input type="text" class=input name="txtHost" value="#strHost#"></td>
    </tr>
    <tr>
      <td width=150> </td>
      <td><input type="submit" class=button name="Submitbutton" value="Query" ></td>
    </tr>
    <tr>
      <td width=150>Country Code:</td>
      <td><input type="text" class=input name="txtCountryCode" value="#strCountryCode#" disabled="true"></td>
    </tr>
    <tr>
      <td width=150>Country Code Ex:</td>
      <td><input type="text" class=input name="txtCountryCodeEx" value="#strCountryCodeEx#" disabled="true"></td>
    </tr>
    <tr>
      <td width=150>Country Name:</td>
      <td><input type="text" class=input name="txtCountryName" value="#strCountryName#" disabled="true"></td>
    </tr>  
    <tr>
      <td width=150>Logfile:</td>
      <td><input type="text" class=input name="txtLogFile" value="#strLogFile#" ></td>
    </tr>
    <tr>
      <td width=150><b>Result:</b></td>
      <td>#strLastError#</td>
    </tr>
  </table>
</form>

<p>This sample is based on ActiveXperts Network Component, an <a target="blank" href="http://www.activexperts.com">ActiveXperts Software</a> product.</p>
<a href="index.cfm">Back to main page</a>


</body>

</html>
</cfoutput>

You can download the complete sample on our FTP site ftp.activexperts-labs.com/samples/network-component/. There are many other working Network Component scripts on our site and shipped with the product.