Click here to Skip to main content
15,895,746 members

Port Mapping in C# is not working

nina4ever asked:

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

C#
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:


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. Why would mapping fail ?
Thanks in advance
Tags: C#, Port mapping, UPnP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900