software products and customization
Contact: support@xuebrothers.net Home page

ActiveSIGA Sample Codes: CSharp

This is a C# Console program.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACTIVESIGALib;

namespace UseSiga
{
    class Program
    {
        static void Main(string[] args)
        {
            //GetExternalIPAddress()
            Siga siga = new Siga();
            String ip = siga.GetExternalIPAddress();
            if (ip.Length < 1)
            {
                Console.Write("GetExternalIPAddress() failed\n" + siga.LastError);
            }
            else
            {
                Console.Write("External IP Address=" + siga.GetExternalIPAddress()+"\n");
            }
            
            //AddPortMapping()
            MapEntry me = new MapEntry();
            me.Protocol = "TCP";
            me.ExternalPort = 12345;
            me.InternalPort = 12345;
            me.InternalClient = "192.168.1.100"; //empty for the caller's IP
            me.PortMappingDescription = "my mapping";
            me.Enabled = true;
            if (siga.AddPortMapping(me))
            {
                Console.Write("Add port mapping OK\n");
            }
            else
            {
                Console.Write("Add port mapping failed:\n"+siga.LastError+"\n");
            }

            //GetGenericPortMappingEntry()
            uint i = 0;
            while (true)
            {
                MapEntry me2 = siga.GetGenericPortMappingEntry(i++);
                if (me2==null) //No more mapped entries
                {
                    break;
                }
                String info = me2.Protocol + " port " + me2.ExternalPort.ToString() + " is mapped to ";
                info += me2.InternalClient;
                info += ":";
                info += me2.InternalPort.ToString();
                if (me2.PortMappingDescription.Length>0)
                {
                    info += ", ";
                    info += me2.PortMappingDescription;
                }
                info += (me2.Enabled ? ", Enabled" : ", Disabled");
                info += "\n";
                Console.Write(info);
            }

            //Delete a Mapping Entry
            MapEntry me3 = new MapEntry();
            me3.Protocol = "TCP";
            me3.ExternalPort = 1234;
            if (siga.DeletePortMapping(me3))
            {
                Console.Write("Delete port mapping OK");
            }
            else
            {
                Console.Write(siga.LastError);
            }


        }
    }
}