1,105,450 Community Members

Port Mapping in C# is not working

Member Avatar
Nina 4 Ever
Newbie Poster
4 posts since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i need to perform port forwarding in a desktop application written in C#.
i used this code :

using System;
    using System.Threading;
    using NATUPNPLib;

    namespace iSpyApplication
    {
        public static class NATControl
        {
            public static UPnPNAT NAT = new UPnPNAT();
            private static IStaticPortMappingCollection _mappings;

            public static IStaticPortMappingCollection Mappings
            {
                get
                {
                   if (_mappings==null)
                   {

                       try
                       {
                           if (NAT.NATEventManager != null)
                            _mappings = NAT.StaticPortMappingCollection;
                       }
                       catch
                       {
                       }
                   }

                   return _mappings;
                }
             }

            public static bool SetPorts(int wanPort, int lanPort)
            {
               bool b = false;
               int i = 3;
               while (Mappings == null && i > 0)
               {
                   Thread.Sleep(2000);
                   i--;
               }

               if (Mappings != null)
               {
                   try
                   {
                       Mappings.Remove(wanPort, "TCP");
                   }
                   catch (Exception ex)
                   {
                       // do something
                   }
                   try
                   {
                       Mappings.Add(wanPort, "TCP", lanPort, internalIP, true, "iSpy");
                       b = true;
                   }
                   catch (Exception ex)
                   {
                       // do something
                   }
               }

               return b;

            } // method

      } // class    

    } // namespace

UPnP is enabled in my Linksys router

the code is running and not giving any errors or exceptions, however, mapping is simply not happening.

here is how i am testing it:

Manually :

  1. i have an application that listens to an internal port e.g. 8080.
  2. i manually add a mapping entry to my router:
    application : HTTP , external port 8080 , internal port 8080, protocol: TCP , internal IP , enabled
  3. i request the following URL in my browser:
    http : // publicIP : externalport /
  4. the application receives a socket on the internal port and replies with a message.

i manually remove the mapping entry from the router and try to add it programmatically:

  1. the same
  2. i run a code that performs mapping
  3. the same
  4. browser displays the following message :

Unable to connect

Firefox can't establish a connection to the server a publicIP:externalPort

i have searched for C# code that performs mapping, and they all look the same.
i downloaded a sample project from here: http://www.codeproject.com/Articles/110338/UpnpStat-List-and-Modify-Port-Mappings-on-your-UPn ... the code is very similar to mine. i ran it, no errors occurred, but it had no effect neither!

Why would mapping fail ?
Thanks in advance

Member Avatar
Momerath
Senior Poster
3,831 posts since Aug 2010
Reputation Points: 1,327 [?]
Q&As Helped to Solve: 664 [?]
Skill Endorsements: 19 [?]
Featured
 
0
 

You have an exception block with an empty catch, put something in there. Check to see if Mappings is null.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

Possible problems:

If you are developing on an Windows XP machine you have to enable UPNP on the computer. see: http://support.microsoft.com/kb/941206

Your firewall may also be blocking access to the router for your program. I think you need ports TCP 2869 and UDP 1900 open.

A good test program to ensure your PC can do this is: http://www.codeproject.com/Articles/13285/Using-UPnP-for-Programmatic-Port-Forwardings-and-N

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article