Take the 2-minute tour ×

I have a sockets program that requires port 1002 to be open and I wanna know how to port forward in C++ on windows so i may use this port freely?

share|improve this question
You will probably need to establish why you can't use this program as-is, and why you believe "port forwarding" is the solution to your problem. –  Greg Hewgill Feb 3 '10 at 3:17
Even if you can port forward your transmissions, you won't be able to get around a banned port or busy port. Change the socket program settings instead. –  shinkou Feb 3 '10 at 3:20
A semi-plausible but probably incorrect way to read this question: "I am trying to make linux.die.net/man/1/forward compile on Windows so I can run another program using a different port than the one it was designed to use." Not sure what it has to do with C++ though. –  bk1e Feb 3 '10 at 7:04
add comment

5 Answers

up vote 6 down vote accepted

Port forwarding is done upstream of the client system, typically on the router.

I believe some applications use Universal Plug and Play to communicate with the upstream router to open a port publicly but you'll have to do a lot of research to see how it's done: I haven't the slightest.

share|improve this answer
add comment

So, you need UPnP or NAT-PMP. Your system may have libraries for doing those protocols, or you can use muniupnp, which has both a server to run on a Linux or BSD box to test against, and a client library.

There is no guarantee that will work, so you may need to go further and use STUN, TURN or ICE. There's a library for doing those things here.

And always remember, sometimes it's just impossible. Implement IPv6 as well, sometimes that works when IPv4 doesn't. Encourage users to install v6 and Teredo.

share|improve this answer
add comment

Starting with Windows XP, Microsoft offers a UPnP library. It's actually fairly complicated to use as the library just offers the basics of communicating with network devices. You will have to study the various UPnP specifications in order to perform device-specific tasks such as port forwarding. Believe me, this is a lot of hard work to do to accomplish relatively little; better to just put it in your documentation somewhere that the user might have to consult their router manual in order to forward the necessary ports. This is what almost everybody does.

share|improve this answer
amen. it's very hard –  Matt Joiner Feb 3 '10 at 5:07
UPnP is hard to use as a programmer, almost as hard as successfully configuring a port forward on a router is for the average "DHCP? what's-an-IP-address?" user. You may as well just have tech support call them, save some time ;-) NATs and firewalls are a great example of a technology that's hostile to good user experience, it's very difficult for the system and the average user to communicate in terms they both understand. –  Steve Jessop Feb 3 '10 at 12:31
add comment

You can't port forward in a programming language, that is something that is set up on the router.

share|improve this answer
UPnP provides an XML API to do this; many (if not all) routers support this out of the box. –  Luke Feb 3 '10 at 4:32
If routers don't port forward in software I'm not sure how they manage it... –  Jonathon Faust Feb 3 '10 at 4:49
@Jon: very cute. Given the OP's level of understanding, I think (as I'm sure you do) my question was helpful. –  Hogan Feb 3 '10 at 11:49
To users who view UPnP as a reason to down vote this answer, remember that any secure windows setup will have UPnP turned off. To expect users to have UPnP on is the same as giving up on supporting the feature. –  Hogan Apr 24 at 14:00
add comment

I believe the most popular approach is to write documentation that tells the user what port(s) to open on their firewall.

share|improve this answer
My experience has been that it's just as common to leave the user to search the internet for forum posts telling them what inbound ports the application uses ;-) –  Steve Jessop Feb 3 '10 at 12:37
add comment

Your Answer


