views:

246

answers:

2

I have a TcpListener object that is operating behind a firewall on port 4000. Obviously, in order for outside clients to connect to the TcpListener, port 4000 needs to be opened in the firewall; otherwise, no outside connection request would get through.

My question has to do with what happens when I accept the connection request like so:

TcpClient client = server.AcceptTcpClient();

The TcpClient is opened on the local interface with a system-assigned port number. Does this port number have to be opened within the firewall in order for the outside client to communicate with this TcpClient instance? If so, is it possible to specify a range of port numbers to use so they can be opened in advance? Or does the firewall automatically allow communication on this system-assigned port number because something behind the firewall (my server, in this case) established, i.e., accepted, the connection?

+1  A: 
  • On the server machine, the firewall needs to allow incoming connections from the port the client is connecting from, to the port the TcpListener is listening on.

  • On the client machine, the firewall needs to allow outgoing connections to the port the TcpListener is listening on, from the port the client is connecting from.

Most firewalls (e.g., Windows Firewall) are configured to allow any outgoing connections to any destination, so you just have to create a rule that allows incoming connections on your port 4000 from any source.

If you want to restrict this more closely, you can bind the TcpClient to a specific port instead of the system-assigned port, and create firewall rules on the server and the client to allow connections only from/to this port.

dtb
A: 

Giving range of ports in windows firewall is not easy but you can grant entire program, when you add new entry in windows firewall, choose program instead of port and grant your program to receive all connections then regardless of which ports you choose, it will always be allowed.

Akash Kava
Yes, that's true. Worst case, that may be the route we go. But I think our IT folks would prefer to lock it down more closely than that if possible.
Matt Davis
We prefer same too, but Microsoft Firewall does not allow you to specify port range, thats absolutely strange, I think they want us to buy their product IAS, i dont know if that has range facility or not, I agree other firewalls do give such support and its the best.
Akash Kava