Click here to Skip to main content
15,117,095 members
Articles / Programming Languages / C++
Posted 24 Jun 2008


39 bookmarked

Easy Port Forwarding and Managing Router with UPnP

Rate me:
Please Sign up or sign in to vote.
2.74/5 (20 votes)
24 Jun 2008CPOL2 min read
This article describes what port forwarding is. UPnP source code is also given.


The article will let you know what port forwarding is and how to add a port forwarding entry into your router with UPnP technology.

What is port forwarding

Port forwarding or port triggering allows remote computers (e.g., public machines on the Internet) to connect to a specific computer within a private LAN.

port forwarding,port forward

Basically, port forwarding works so that you can set your router to let internet users access your computer that is behind your router. You router has a public IP and your computer just has a private IP. Internet users can just directly access your router, but not your computer. So if you are using utorrent or other P2P software and if you want get a high download speed, you must let internet users directly access your computer. Then, you must set your router and add port mapping. Often, you must login into your router's administrator page and manually add port mapping. If you download movies or files from the internet using azureus, utorrent, or BitComet and want a very high download speed, you must know how to operate your router to add or delete port mappings.

Manual port forwarding vs. auto port forwarding

Anyone can configure a router for port forwarding, but it's a manual process that's usually not simple, and it's a process that differs for each different model of router. There are a few websites out there that centralizes instructions for configuring most common routers -- the best of these is probably Take a look at that site, and you'll see the great variety in instructions for configuring routers to port-forward incoming connections. The UPnP technology makes it easier to configure routers for port forwarding and NAT traversal, since the configuration can be done programmatically for you, without needing manual steps from you.

What is UPnP?

Universal Plug and Play (UPnP) is a set of computer network protocols promulgated by the UPnP Forum. The goals of UPnP are to allow devices to connect seamlessly and to simplify the implementation of networks in home (data sharing, communications, and entertainment) and corporate environments. UPnP achieves this by defining and publishing UPnP device control protocols built upon open, Internet-based communication standards. If you want to buy a router, you should check the specification: if you don't see UPnP supported, then throw it away:)

How to implement a UPnP engine?

Actually, UPnP is just a SOAP message. You can check the attachment and find the source code to learn how to add a port forwarding entry into a router.

#ifndef UPNPNAT_H
#define UPNPNAT_H

#include <string>
#include <vector>

#pragma   warning(disable:   4251) 

#define DefaultTimeOut  10
#define DefaultInterval 200

class __declspec (dllexport) UPNPNAT
    bool init(int time_out=DefaultTimeOut,int interval=DefaultInterval); //init
    bool discovery();//find router

     **** _description: port mapping name
     **** _destination_ip: internal ip address
     **** _port_ex:external: external listen port
     **** _destination_port: internal port
     **** _protocal: TCP or UDP
    bool add_port_mapping(
             char * _description, char * _destination_ip, unsigned short int _port_ex,
             unsigned short int _destination_port, char * _protocal);//add port mapping

    const char * get_last_error(){ return last_error.c_str();}//get last error
    bool get_description();            //
    bool parser_description();        //
    bool tcp_connect(const char * _addr,unsigned short int _port);
    bool parse_mapping_info();
    int udp_socket_fd;
    int tcp_socket_fd;
    typedef enum 
    } NAT_STAT;
    NAT_STAT status;
    int time_out;
    int interval;
    std::string service_type;
    std::string describe_url;
    std::string control_url;
    std::string base_url;
    std::string service_describe_url;
    std::string description_info;
    std::string last_error;
    std::string mapping_info;


Here is the demo source code to learn how to use the UPnp engine:

#include "upnpnat.h"

int main (int argc,char * argv[]){
    UPNPNAT nat;

        printf("discovery error is %s\n",nat.get_last_error());
        return -1;
        printf("add_port_mapping error is %s\n",nat.get_last_error());
        return -1;
    printf("add port mapping succ.\n");

    return 0;


Here is a good port forwarding tool and port forwarding blog for you.


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


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

QuestionUnresolved external symbol Pin
Rafael Davidson19-Jan-16 2:56
MemberRafael Davidson19-Jan-16 2:56 
GeneralMy vote of 5 Pin
JOE Heart Under Blade19-Dec-13 14:51
MemberJOE Heart Under Blade19-Dec-13 14:51 
GeneralRe: My vote of 5 Pin
Peter Eugene Coleman27-Mar-14 2:36
MemberPeter Eugene Coleman27-Mar-14 2:36 
GeneralRe: My vote of 5 Pin
JOE Heart Under Blade1-Apr-14 15:49
MemberJOE Heart Under Blade1-Apr-14 15:49 
QuestionUPNP Pin
Peter Eugene Coleman30-Oct-13 5:55
MemberPeter Eugene Coleman30-Oct-13 5:55 
QuestionI'm a newbie and I might be missing something Pin
Peter Eugene Coleman28-May-13 12:35
MemberPeter Eugene Coleman28-May-13 12:35 
QuestionUpnp enabled Gateway simulation on a windows XP PC Pin
Member 972771917-Jan-13 3:34
MemberMember 972771917-Jan-13 3:34 
QuestionWhat Language is this? Pin
bEGI2328-Oct-12 11:24
MemberbEGI2328-Oct-12 11:24 
Generaldiscovery error is Fail to find an UPNP NAT. Pin
Sebastian__17-May-11 9:29
MemberSebastian__17-May-11 9:29 
GeneralMy vote of 1 Pin
brianbacon20-Jan-11 16:49
Memberbrianbacon20-Jan-11 16:49 
Question"Double forwarding" Pin
Member 330389117-Aug-09 11:39
MemberMember 330389117-Aug-09 11:39 
AnswerRe: "Double forwarding" Pin
ERAI5-Sep-10 4:55
MemberERAI5-Sep-10 4:55 
QuestionThanks Pin
doug655364-Aug-08 21:01
Memberdoug655364-Aug-08 21:01 
AnswerRe: Thanks Pin
upredsun5-Aug-08 0:47
Memberupredsun5-Aug-08 0:47 
Generalplease check code formatting... Pin
Emilio Garavaglia25-Jun-08 22:12
MemberEmilio Garavaglia25-Jun-08 22:12 
GeneralRe: please check code formatting... Pin
upredsun25-Jun-08 22:20
Memberupredsun25-Jun-08 22:20 
GeneralRe: please check code formatting... Pin
Emilio Garavaglia26-Jun-08 21:55
MemberEmilio Garavaglia26-Jun-08 21:55 
GeneralSome suggestions Pin
Ed.Poore25-Jun-08 1:57
MemberEd.Poore25-Jun-08 1:57 
GeneralRe: Some suggestions Pin
Peter Eugene Coleman28-May-13 12:28
MemberPeter Eugene Coleman28-May-13 12:28 
GeneralRe: Some suggestions Pin
Hans77Lindgren22-Feb-14 0:57
MemberHans77Lindgren22-Feb-14 0:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.