I would like to make a program in C# that would port forward a specific port from the router to the computer. Is this possible?
2 Answers
It is possible to forward a port using UPNP
(assuming your device supports the technology) using NATUPnP 1.0 Type Library (NATUPNP.DLL) or a third party library like Mono.Nat.
If you want to do it without UPNP then it will be considerably harder since you will need to find what kind of message you will need to send to the device (router) to simulate a user adding a port.
For Mono.Nat
you can find an example at UPNP port forwarding – The easy way
and for NATUPnP
there is an example at .NET Framework: Communicate through NAT Router via UPnP (Universal Plug and Play)

- 4,427
- 2
- 33
- 45
-
Thanks for replying in detail, in fact the application is something like the SERVER(on Network A), connecting to CLIENT1(on Network B), sends a packet and end the UDP. Will UPNP be applicable? – Mar 25 '13 at 12:30
-
Yes, assuming the client's device supports UPNP (most devices do support it nowadays but there are always exceptions). – coolmine Mar 25 '13 at 12:32
-
well, allow me to put my question in this way, my server is going to send a packet remotely via udp, is it going to be blocked by the client's router? if so, how should i bypass it? – Mar 25 '13 at 12:37
-
If the client is behind a firewall then it will be blocked yes. In order to bypass it you will need to make sure the port you are sending the information from the server is open on the client's machine. Also with UDP there is another method called "UDP hole punching", you can find some extra information about it here if you are interested for a third option http://stackoverflow.com/questions/9140450/udp-hole-punching-implementation – coolmine Mar 25 '13 at 12:40
No, it is not possible
Port forwarding is the task of your network device where all the other computers of a network are connected. Once the packet leaves the router, it has no control over it.
Further, unless you forward the port from router, there is no way for a computer in internal lan to get that packet and there is no question of forwarding at all.

- 6,438
- 6
- 27
- 34
-
6
-
@Bart Friederichs uPnP is not port forwarding, it is a different technology. – Murtuza Kabul Mar 25 '13 at 12:19
-
-
Hi, thanks for responding, my project requires an external connection to a specific computer by UDP via a port, thus the user has to enable the port forwarding to the computer manually. However I would want this be done in the program automatically, is this possible? – Mar 25 '13 at 12:22
-
It is possible, however, both the client and server, i.e. your computer and the remote computer should be able to use uPnP. It is a protocol which allows dynamically discovering client and server. However, you will not be able to forward a port though. Further, let me remind you that most of the routers do not allow uPnP also by default so with uPnP, there is no guarantee that your application will work – Murtuza Kabul Mar 25 '13 at 12:25
-
Check out the netgear page http://documentation.netgear.com/fvg318/enu/202-10318-02/FVG318_RMv2-10-12.html – Murtuza Kabul Mar 25 '13 at 12:27
-
2@MurtuzaKabul Incorrect about the port forwarding: http://en.wikipedia.org/wiki/Universal_Plug_and_Play#NAT_traversal About the support, yes that could be a thing. – Bart Friederichs Mar 25 '13 at 12:27
-
@Bart - that is what uPnP is and it is not possible unless you allow it from router. I have already said in my comments. Further the question was for port forwarding which is entirely different subject. – Murtuza Kabul Mar 25 '13 at 12:30
-
Aso mind that many ISP provide you an IP which is not live, i.e. an IP behind NAT and they do not like to provide such facilities. Best option is to ensure that the communication is initiated from the remote computer. – Murtuza Kabul Mar 25 '13 at 12:34