TCP port forwarding without any tools using the “netsh interface portproxy” command

Posted: (EET/GMT+2)

 

When developing networked applications, there've been countless times when I've needed to create temporary port forwardings so that I can more easily test and debug networking applications using TCP/IP.

So far, I've used simple utilities that can be found from the Internet, or have written my own apps with .NET and C#. However, I recently learned that if you are running Windows 7 and/or Windows Server 2008 or later (includes Windows 8 and Windows Server 2008 R2 and 2012 as well) already built-in commands for setting up port forwarding. What's more, the build-in command can also create proxies between IPv4 and IPv6 worlds, so you could have an IPv4-only client talk directly to an IPv6-only server, etc.

The command you need is the venerable NetSh command, accessible from an elevated command prompt ("Run as Administrator"). More specifically, you need the "netsh interface portproxy" context of the command (NetSh is one of the largest command-line commands available in Windows).

For instance, to forward "localhost" port 5000 to another computer/server on port 80, you would run the following command (note that you must type it on a single line):

netsh interface portproxy add v4tov4 listenport=5000
listenaddress=localhost connectaddress=www.microsoft.com connectport=80

In this case, the forwarding would connect your localhost:5000 to www.microsoft.com. Indeed, after running the command, you could browse Microsoft's web site through http://localhost:5000/.

There are many different options on the command, so I suggest you browse through the reference (see link below). If you want to list the active port forwardings, you can use the following command:

netsh interface portproxy show all

This lists something like this:

C:\Windows\system32>netsh interface portproxy show all

Listen on ipv4:             Connect to ipv4:

Address         Port        Address           Port
--------------- ----------  ---------------   ----------
localhost       5000        www.microsoft.com 80

For a complete command reference, see Technet.