API Reference

Version 3 (Alan McGovern, 04/16/2009 10:09 PM)

1 1
h1. API Reference
2 1
3 2 Alan McGovern
<pre>
4 3 Alan McGovern
    class NatTest
5 3 Alan McGovern
    {
6 3 Alan McGovern
        public Start ()
7 3 Alan McGovern
        {
8 3 Alan McGovern
            // Hook into the events so you know when a router has been detected or has gone offline
9 3 Alan McGovern
            NatUtility.DeviceFound += DeviceFound;
10 3 Alan McGovern
            NatUtility.DeviceLost += DeviceLost;
11 1
12 3 Alan McGovern
            // Start searching for upnp enabled routers
13 3 Alan McGovern
            NatUtility.StartDiscovery ();
14 3 Alan McGovern
        }
15 3 Alan McGovern
16 3 Alan McGovern
        void DeviceFound(object sender, DeviceEventArgs args)
17 1
        {
18 3 Alan McGovern
            // This is the upnp enabled router
19 1
            INatDevice device = args.Device;
20 1
         
21 1
            // Create a mapping to forward external port 3000 to local port 1500
22 1
            device.CreatePortMap(new Mapping(Protocol.Tcp, 1500, 3000));
23 1
24 1
            // Retrieve the details for the port map for external port 3000
25 1
            Mapping m = device.GetSpecificMapping(Protocol.Tcp, 3000);
26 1
27 1
            // Get all the port mappings on the device and delete them
28 1
            foreach (Mapping mp in device.GetAllMappings())
29 1
                device.DeletePortMap(mp);
30 1
31 1
            // Get the external IP address
32 1
            IPAddress externalIP = device.GetExternalIP();
33 1
        }
34 1
		
35 3 Alan McGovern
        private void DeviceLost (object sender, DeviceEventArgs args)
36 3 Alan McGovern
        {
37 3 Alan McGovern
            INatDevice device = args.Device;
38 1
			
39 3 Alan McGovern
            Console.WriteLine ("Device Lost");
40 3 Alan McGovern
            Console.WriteLine ("Type: {0}", device.GetType().Name);
41 3 Alan McGovern
        }
42 3 Alan McGovern
    }
43 2 Alan McGovern
</pre>