Download ActiveSocket Network Communications Toolkit 4.1  (5094 KB - .exe file)
Download Manual  (505 KB - .htm file)
Visual C# .NET SNMP 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), DNS, IP to country lookup, WOL (Wake-On-LAN), and more.
SNMP can be well integrated into Visual C# .NET environments.
This document describes how ActiveSocket's SNMP objects can be integrated into Visual C# .NET projects.
ActiveSocket is compliant with SNMP v1 and SNMP v2c. ActiveSocket automatically detects which SNMP version is running on the remote agent.
Different SNMP data types are supported, including:
- String types (also called "octet strings");
- Integer types (16bit, 32bit, 64bit and unsigned integers);
- IP Address types;
- Timetick types;
- Counter types (32bit and 64bit counters);
- OID types (also called "Object ID's");
- Other, less frequently used datatypes.
The following operations are supported:
- Get - retrieve an object variable from the (remote) agent;
- GetNext - retrieve the next object variable from a table or list within an agent;
- Set - set values for object variables within an agent.
Step 1: Download and install the ActiveSocket Toolkit
Download the 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 Visual C# .NET Project
Launch Microsoft Visual Studio (for instance 'Microsoft Visual Studio 2003') from the Start menu.
Choose 'New' from the 'File' menu and click on 'Project'. In the 'New Project' dialog, select a Visual Studio template (for instance: 'Console Application').
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 SnmpManager m_objSnmpManager;
public SocketConstants m_objConstants;
m_objSnmpManager = new SnmpManager();
m_objConstants = new SocketConstants();
Step 4: Creating a simple SNMP manager application
After the project has been created, and the objects has been declared and created, you can add the rest of the code to build a simple SNMP manager application.
You can find the sourcecode of this application below:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ASOCKETLib;
namespace Demo
{
/// <summary>
/// Summary description for SnmpForm.
/// </summary>
public class SnmpForm : System.Windows.Forms.Form
{
internal System.Windows.Forms.GroupBox GroupBox3;
internal System.Windows.Forms.TextBox TextResult;
internal System.Windows.Forms.Label Label9;
internal System.Windows.Forms.GroupBox GroupBox2;
internal System.Windows.Forms.TextBox TextType;
internal System.Windows.Forms.Label Label10;
internal System.Windows.Forms.Button ButtonSet;
internal System.Windows.Forms.Button ButtonGetNext;
internal System.Windows.Forms.Button ButtonGet;
internal System.Windows.Forms.TextBox TextNewValue;
internal System.Windows.Forms.Label Label8;
internal System.Windows.Forms.TextBox TextValue;
internal System.Windows.Forms.Label Label7;
internal System.Windows.Forms.Label Label6;
internal System.Windows.Forms.TextBox TextOID;
internal System.Windows.Forms.Label Label5;
internal System.Windows.Forms.GroupBox GroupBox1;
internal System.Windows.Forms.ComboBox ComboVersion;
internal System.Windows.Forms.Button ButtonClose;
internal System.Windows.Forms.Button ButtonOpen;
internal System.Windows.Forms.Label Label4;
internal System.Windows.Forms.TextBox TextPort;
internal System.Windows.Forms.Label Label3;
internal System.Windows.Forms.TextBox TextCommunity;
internal System.Windows.Forms.TextBox TextAgent;
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label Label1;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.TextBox TextLogFile;
private System.Windows.Forms.Button buttonView;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public SnmpForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
#endregion
private SnmpManager objSnmpManager;
private SocketConstants objConstants;
private bool bOpened;
private void SnmpForm_Load(object sender, System.EventArgs e)
{
objSnmpManager = new SnmpManager();
objConstants = new SocketConstants();
objSnmpManager.Initialize();
bOpened = false;
TextLogFile.Text = System.IO.Path.GetTempPath() + "SnmpLog.txt";
EnableControls();
}
private void ButtonOpen_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
objSnmpManager.ProtocolVersion = ComboVersion.SelectedIndex + 1;
objSnmpManager.LogFile = TextLogFile.Text;
objSnmpManager.Open(TextAgent.Text, TextCommunity.Text, Int16.Parse(TextPort.Text));
if ( GetResult() == 0 )
{
bOpened = true;
EnableControls();
}
Cursor.Current = Cursors.Default;
}
private void ButtonClose_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
objSnmpManager.Close();
if ( GetResult() == 0 )
{
bOpened = false;
EnableControls();
}
Cursor.Current = Cursors.Default;
}
private void ButtonGet_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
SnmpObject objSnmpObject;
objSnmpObject = ( SnmpObject ) objSnmpManager.Get(TextOID.Text);
if ( GetResult() == 0 )
{
TextValue.Text = objSnmpObject.Value;
TextOID.Text = objSnmpObject.OID;
TextType.Text = GetValueType(objSnmpObject.Type);
}
Cursor.Current = Cursors.Default;
}
private void ButtonGetNext_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
SnmpObject objSnmpObject;
objSnmpObject = ( SnmpObject ) objSnmpManager.GetNext();
if ( GetResult() == 0 )
{
TextValue.Text = objSnmpObject.Value;
TextOID.Text = objSnmpObject.OID;
TextType.Text = GetValueType(objSnmpObject.Type);
}
Cursor.Current = Cursors.Default;
}
private void ButtonSet_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
SnmpObject objSnmpObject = new SnmpObject();
objSnmpObject.Clear ();
objSnmpObject.Value = TextNewValue.Text;
objSnmpObject.OID = TextOID.Text;
objSnmpObject.Type = GetTypeAsLong ( TextType.Text );
object obj = objSnmpObject;
objSnmpManager.Set ( ref obj );
GetResult ();
Cursor.Current = Cursors.Default;
}
private int GetResult ()
{
TextResult.Text = objSnmpManager.LastError + " : " + objSnmpManager.GetErrorDescription(objSnmpManager.LastError);
return objSnmpManager.LastError;
}
private void EnableControls ()
{
ButtonGet.Enabled = bOpened;
ButtonSet.Enabled = bOpened;
ButtonGetNext.Enabled = bOpened;
ButtonClose.Enabled = bOpened;
ButtonOpen.Enabled = !bOpened;
TextOID.Enabled = bOpened;
TextNewValue.Enabled = bOpened;
}
private string GetValueType (int lType)
{
string strType = "";
if ( lType == objConstants.asSNMP_TYPE_BITS ) strType = "ASN_BITS";
if ( lType == objConstants.asSNMP_TYPE_COUNTER32) strType = "ASN_COUNTER32";
if ( lType == objConstants.asSNMP_TYPE_COUNTER64) strType = "ASN_COUNTER64";
if ( lType == objConstants.asSNMP_TYPE_TIMETICKS) strType = "ASN_TIMETICKS";
if ( lType == objConstants.asSNMP_TYPE_OCTETSTRING) strType = "ASN_OCTETSTRING";
if ( lType == objConstants.asSNMP_TYPE_GAUGE32) strType = "ASN_GAUGE32";
if ( lType == objConstants.asSNMP_TYPE_IPADDRESS) strType = "ASN_IPADDRESS";
if ( lType == objConstants.asSNMP_TYPE_OPAQUE) strType = "ASN_OPAQUE";
if ( lType == objConstants.asSNMP_TYPE_UNSIGNED32) strType = "ASN_UNSIGNED32";
if ( lType == objConstants.asSNMP_TYPE_OBJECTIDENTIFIER) strType = "ASN_OBJECTIDENTIFIER";
if ( lType == objConstants.asSNMP_TYPE_NULL) strType = "ASN_NULL";
if ( lType == objConstants.asSNMP_TYPE_INTEGER) strType = "ASN_INTEGER";
if ( lType == objConstants.asSNMP_TYPE_INTEGER32) strType = "ASN_INTEGER32";
if ( lType == objConstants.asSNMP_TYPE_SEQUENCE) strType = "ASN_SEQUENCE";
return strType;
}
private int GetTypeAsLong (string strType )
{
int lType = 0;
switch ( strType )
{
case "ASN_INTEGER": lType = objConstants.asSNMP_TYPE_INTEGER; break;
case "ASN_BUTS": lType = objConstants.asSNMP_TYPE_BITS; break;
case "ASN_OCTETSTRING": lType = objConstants.asSNMP_TYPE_OCTETSTRING; break;
case "ASN_NULL": lType = objConstants.asSNMP_TYPE_NULL; break;
case "ASN_OBJECTIDENTIFIER": lType = objConstants.asSNMP_TYPE_OBJECTIDENTIFIER; break;
case "ASN_INTEGER32": lType = objConstants.asSNMP_TYPE_INTEGER32; break;
case "ASN_SEQUENCE": lType = objConstants.asSNMP_TYPE_SEQUENCE; break;
case "ASN_IPADDRESS": lType = objConstants.asSNMP_TYPE_IPADDRESS; break;
case "ASN_COUNTER32": lType = objConstants.asSNMP_TYPE_COUNTER32; break;
case "ASN_GAUGE32": lType = objConstants.asSNMP_TYPE_GAUGE32; break;
case "ASN_TIMETICKS": lType = objConstants.asSNMP_TYPE_TIMETICKS; break;
case "ASN_OPAQUE": lType = objConstants.asSNMP_TYPE_OPAQUE; break;
case "ASN_COUNTER64": lType = objConstants.asSNMP_TYPE_COUNTER64; break;
case "ASN_UNSIGNED32": lType = objConstants.asSNMP_TYPE_UNSIGNED32; break;
}
return lType;
}
private void buttonView_Click(object sender, System.EventArgs e)
{
if (System.IO.File.Exists(TextLogFile.Text.ToString()))
{
System.Diagnostics.Process.Start(TextLogFile.Text);
}
}
}
}
You can download the complete sample on our ftp site ftp.activexperts-labs.com/samples/asocket.
There are many other working ActiveSocket scripts on our site and shipped with the product.
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.
|