ActiveSocket

 Product Overview

 ActiveSocket Objects:
 
 How to use

 Online Samples

 Download (.exe)

 Brochure (.pdf)

 Manual (.htm)

 Release Notes


Support

 Knowledge Base

 Forum

 Contact Support


Purchase

 Licensing

 Pricing

 Order now


Related documents

 Tutorials

 Tools


  Download ActiveSocket Network Communications Toolkit 4.1  (5094 KB - .exe file)
  Download Manual  (505 KB - .htm file)


ASP .NET (C#) IPtoCountry Sample Source Code


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

ActiveSocket features the following: ICMP, HTTP and HTTPs with support for proxy servers and secure web sites, Telnet, NTP time protocol, RSH remote shell script interface, SNMP (Simple Network Management Protcol), SNMP Traps, Sockets (TCP and UDP), WOL (Wake-On-LAN), IP address to country lookup and more.

ActiveSocket can be well integrated into ASP.NET environments.
This document describes how ActiveSocket can be integrated into into ASP .NET (C#) projects.


Prerequisites

You must install and configure Internet Information Services (IIS) before using the ActiveSocket Toolkit with ASP .NET
If you don't have IIS installed, use the following steps:
  • From the Control Panel, click 'Add/Remove Programs'. Select the 'Add/Remove Windows Components' icon from the left pane, then select 'Application Server' and click on 'Details'. You can now select both 'ASP .NET' and 'Internet Information Services (IIS)'. Click 'OK' to continue installation;
  • Make sure that ASP .NET is allowed on the web server:


    (Click on the picture to enlarge)



Step 1: Download and install ActiveSocket

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



Step 2: Create a new ASP .NET C# Project

Launch Microsoft Visual Studio (for instance 'Microsoft Visual Studio 2005') from the Start menu. Choose 'New' from the 'File' menu and click on 'Web Site'. In the 'Web Site' dialog, select ASP .NET Web Site. Select a name for the application (for instance: 'DemoApp') and a name for the solution (for instance: 'DemoSolution'). Also, select the directory where you want to store the project (for instance: 'C:\MyProjects):

    
    (Click on the picture to enlarge)



Step 3: Refer to the ActiveSocket Library and create the objects

Now that a new project has been created, you must add a reference to the ActiveSocket toolkit in the project to be able to use the ActiveSocket object. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the 'ActiveSocket 3.1 Type Library' as shown in the following picture:

    
    (Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

On top of your code, type the following line to use the ActiveSocket namespace:
   using ASOCKETLib;
In your Main function, declare and create the following object:
   public IPtoCountry m_objIPtoCountry;

   m_objIPtoCountry = new IPtoCountry ();


Step 4: Establish a connection to a remote server

You can now establish a connection with a remote server.

The following code shows how to create a simple Telnet client using ASP.NET:

webform1.aspx

<%@ Page language="c#" Codebehind="frmIpToCountry.aspx.cs" AutoEventWireup="false" Inherits="IPtoCountry.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <style type="text/css">
    BODY { FONT-WEIGHT: normal; FONT-SIZE: x-small; COLOR: navy; FONT-FAMILY: verdana }
    H2 { COLOR: navy; FONT-FAMILY: verdana }
    H3 { COLOR: navy; FONT-FAMILY: verdana }
    INPUT { BORDER-RIGHT: navy 1px solid; BORDER-TOP: navy 1px solid; FONT-SIZE: x-small; BORDER-LEFT: navy 1px solid; COLOR: navy; BORDER-BOTTOM: navy 1px solid; FONT-FAMILY: verdana; BACKGROUND-COLOR: white }
  </style>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="WebForm1" method="post" runat="server">
   <asp:TextBox id="txtHostname" style="Z-INDEX: 101; LEFT: 192px; POSITION: absolute; TOP: 92px" runat="server" Width="328px" Height="22px">http://forum.activexperts.com</asp:TextBox>
   <asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 29px; POSITION: absolute; TOP: 169px" runat="server" Width="117px" Height="15px" CssClass="lblInfo">Result:</asp:Label>
   <asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 28px; POSITION: absolute; TOP: 145px" runat="server" Width="117px" Height="15px" CssClass="lblInfo">Country code:</asp:Label>
   <asp:Label id="lblHostname" style="Z-INDEX: 102; LEFT: 27px; POSITION: absolute; TOP: 94px" runat="server" Width="156px" Height="21px" CssClass="lblInfo">Hostname or address:</asp:Label>
   <asp:TextBox id="txtCountry" style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 118px" runat="server" Width="456px" Height="22px" Enabled="False"></asp:TextBox>
   <asp:Label id="lblCountry" style="Z-INDEX: 104; LEFT: 28px; POSITION: absolute; TOP: 119px" runat="server" Width="117px" Height="15px" CssClass="lblInfo">Country:</asp:Label>
   <asp:Button id="btnGetCountry" style="Z-INDEX: 105; LEFT: 521px; POSITION: absolute; TOP: 92px" runat="server" Width="127px" Height="22px" Text="GetCountry:"></asp:Button>
   <asp:TextBox id="txtCountryCode" style="Z-INDEX: 106; LEFT: 192px; POSITION: absolute; TOP: 144px" runat="server" Width="132px" Height="22px" Enabled="False"></asp:TextBox>
   <asp:TextBox id="txtResults" style="Z-INDEX: 109; LEFT: 192px; POSITION: absolute; TOP: 169px" runat="server" Width="456px" Height="22px" Enabled="False"></asp:TextBox>
   <asp:Label id="lblHeader" style="Z-INDEX: 110; LEFT: 110px; POSITION: absolute; TOP: 12px" runat="server" Width="479px" Height="34px" CssClass="lblHeader">
    <h2>ActiveSocket IP to Country Sample</h2>
   </asp:Label>
   <asp:Label id="lblInASP" style="Z-INDEX: 111; LEFT: 256px; POSITION: absolute; TOP: 51px" runat="server" Width="158px" Height="24px">
    <h3>in ASP.NET C#</h3>
   </asp:Label>
  </form>
 </body>
</HTML>


WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ASOCKETLib;

namespace IPtoCountry
{
  /// <summary>
  /// Summary description for WebForm1.
  /// </summary>
  /// 

  public class WebForm1 : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.TextBox txtHostname;
    protected System.Web.UI.WebControls.TextBox txtCountry;
    protected System.Web.UI.WebControls.Label lblCountry;
    protected System.Web.UI.WebControls.Button btnGetCountry;
    protected System.Web.UI.WebControls.TextBox txtCountryCode;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.TextBox txtResults;
    protected System.Web.UI.WebControls.Label lblHeader;
    protected System.Web.UI.WebControls.Label lblInASP;
    protected System.Web.UI.WebControls.Label lblHostname;

    IPtoCountry objIpToCountry;

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }

    private void Page_Load(object sender, System.EventArgs e)
    {
    
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.btnGetCountry.Click += new System.EventHandler(this.btnGetCountry_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void btnGetCountry_Click(object sender, System.EventArgs e)
    {

      //declare variables
      string strAddress;
      string strErrorDescription;
      int intLastError;

      /*ActiveSocket can't process something like http://blabla or ftp://
        So replace all characters and expressions like that, otherwise activeSocket will return an error message*/
      strAddress = txtHostname.Text;
      strAddress = strAddress.Replace("http://", "");
      strAddress = strAddress.Replace("https://", "");
      strAddress = strAddress.Replace("ftp://", "");
      strAddress = strAddress.Replace("'", "");
      //And so on
      //

      //Create the Socket object and set some properties
      objIpToCountry          = new IPtoCountry();

      objIpToCountry.Clear();

      objIpToCountry.LogFile  = "C:/ASocketLog.txt";
      objIpToCountry.Host     = strAddress;

      objIpToCountry.Query();

      if(objIpToCountry.LastError == 0)
      {
        /*the script can only resume if the host address is succesfully entered and the
          query has been executed succesfully */
        txtCountryCode.Text = objIpToCountry.CountryCode;
        txtCountry.Text     = objIpToCountry.CountryName;
        intLastError        = objIpToCountry.LastError;
        strErrorDescription = objIpToCountry.GetErrorDescription(objIpToCountry.LastError);
      }
      else
      {
        /*otherwise, don't execute the query, just return the error and the errormessage
          the result will be countrycode: "??", country: "UNKNOWN" */
        txtCountryCode.Text = "??";
        txtCountry.Text     = "UNKNOWN";
        intLastError        = objIpToCountry.LastError;
        strErrorDescription = objIpToCountry.GetErrorDescription(objIpToCountry.LastError);
      }
      if(txtCountry.Text == "")
      {
        /*if the query has executed succesfully it might be, activeSocket was not able to locate
          the address you entered, in that case activeSocket will return an errormessage
          and I will print ?? UNKNOWN to the txtCountry and txtCountryCode*/
        txtCountryCode.Text = "??";
        txtCountry.Text     = "UNKNOWN";
      }
      //when the results you're finished using the object, reset the variable
      objIpToCountry = null;

      //Print the results
      txtResults.Text           = intLastError.ToString() + " : " + strErrorDescription;
    }
  }
}
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.


NOTE: Demo Projects are created with Microsoft Visual Studio 2002

The project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft ASP .NET C#. The projects are created with Microsoft Visual Studio 2002.
Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.






The ActiveSocket tool is a Network Communications ActiveX software component (SDK). This control supports SNMP, SMTP, POP3, Telnet, TCP, NTP, RSH, HTTP, HTTPs, FTP, DNS, ICMP and more, and can be used by any Windows development platform, including Visual Basic .NET, Visual CSharp .NET, ASP .NET (VB,CS), ASP, Visual Basic, Visual Studio/Visual C++, Delphi, PHP, ColdFusion, HTML, VBScript and any other ActiveX/COM compliant platform. The ActiveSocket Toolkit is an ActiveXperts Software B.V. Product.

Copyright ©1999-2007 ActiveXperts Software. All rights reserved.