Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# port-mapping UPnP
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
               if (_mappings==null)
                       if (NAT.NATEventManager != null)
                        _mappings = NAT.StaticPortMappingCollection;
               return _mappings;
        public static bool SetPorts(int wanPort, int lanPort)
           bool b = false;
           int i = 3;
           while (Mappings == null && i > 0)
           if (Mappings != null)
                   Mappings.Remove(wanPort, "TCP");
               catch (Exception ex)
                   // do something
                   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
Posted 19-Nov-12 11:44am
Edited 19-Nov-12 11:47am
Christian Graus at 19-Nov-12 16:47pm
you're using a third party library, so your code is not really doing the work. That makes it hard to comment
nina4ever at 20-Nov-12 13:29pm
thanks for your reply. i downloaded a sample project from here: ...
the code is very similar to mine. i ran it, no errors occurred, but it had no effect neither!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 495
1 Sergey Alexandrovich Kryukov 469
2 OriginalGriff 360
3 Sascha Lefévre 255
4 Abhinav S 250
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076

Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100