TCP Client Port Forwarding

Started by
10 comments, last by VBStrider 16 years, 9 months ago
While behind a router, some form of port forwarding is required for any inbound packet to get to it's destination. My question is why do TCP clients seem to "magically" forward themselves? Does the router simply automatically forward any outbound TCP connections (and only outbound TCP connections)? VBStrider [Edited by - VBStrider on July 12, 2007 10:29:16 AM]
Advertisement
You are correct in essence. A lot of firewalls only block incoming traffic. When an outbound packet is sent, the firewall registers this as being a change in the state of what types of packets need to be allowed back, and which internal IP address it needs to send those packets to.

This is not specific to TCP packets, as it should also apply to UDP and ICMP packets in most default configurations.
Why does StarCraft require it's UDP port to be forwarded then?

By the way, I just noticed that something like this was asked not too long ago: http://www.gamedev.net/community/forums/topic.asp?topic_id=451659 so sorry if this is spam.

VBStrider
To expand a little on what taby said:
When a TCP client starts up, the first thing it does it send a packet to the server (A TCP SYN packet I belive). The router sees this packet going from the client to the remote machine, and so it notes own in an internal table it has the source and destination IP addresses and ports.

Next, the server replies (with a TCP ACK packet I belive) to the IP address and port it came from. The router recieves this packet, and looks at the IP and port it came from. It sees that it's coming from a IP:port that was used by the client machine earlier, and so it auto-forwards the packet to the IP:port of the client.

Client machine recieves packet and does whatever it wants with it.

The same is possible with UDP as well, it's not just limited to TCP.
Quote:Original post by VBStrider
Why does StarCraft require it's UDP port to be forwarded then?
I'd assume that it's only needed if you're hosting a server on your machine. Because then the first packet sent would be TO the machine behind the router, not FROM it.

This is also needed if you're playing peer-to-peer, because it's entirely possible that both players will be behind a router, so one of them (the server / host) will have to have the port forwarded correctly.

I've never played Starcraft, so I've no idea if this is correct with respect to peer-to-peer and whatnot.

Some commercial games can get around you having to forward the port by either hosting the server themselfs (So they do the port forwarding), or by using NAT punchthrough (Google for this - although it's only really applicable to UDP).
How are you able to connect to the same game from the same LAN on two different computers then? Does windows just work with the router to make sure that it chooses a client port that is not used on any other computer on the network?

Also, I am unable to player StarCraft through Battle.net with a friend on my LAN because only one of us actually gets to play. The other gets severe "lag", can't play really and can't read chat messages... But can connect.

VBStrider
Quote:Original post by VBStrider
How are you able to connect to the same game from the same LAN on two different computers then? Does windows just work with the router to make sure that it chooses a client port that is not used on any other computer on the network?
The router sees that both clients are on the same side of it, and lets them connect directly. When you connect to a remote machine, Windows choses the port (Usually, unless the app tells it not to). On a LAN with two PCs, one will be the server, and will be listening for connections on the game port (Say, port 1234), and the other PC will choose a random port (say 60000) to connect to the first PC. The hosting PC then creates a new connection on a random port (Which doesn't have to be different from the connecting PC, but usually is), and you have a connection between the two PCs.
Even if both PCs manage to choose the same random port, it'll still work, because the IP:port pair is still unique.

Quote:Original post by VBStrider
Also, I am unable to player StarCraft through Battle.net with a friend on my LAN because only one of us actually gets to play. The other gets severe "lag", can't play really and can't read chat messages... But can connect.
Sounds like a problem with the game more than anything else to be honest.
You might want to read the resources on NAT in the Forum FAQ for more enlightenment on this topic.
enum Bool { True, False, FileNotFound };
Quote:The router sees that both clients are on the same side of it, and lets them connect directly.

That's not what I meant... I mean if two people are playing the same game on the same LAN. So for instance, lets say a group of people want to play World of Warcraft together over a single LAN. They are not connected to each other, they are connected to a remote server.

After reading http://www.mindcontrol.org/~hplus/nat-punch.html it appears as though the router uses one of it's own ports, so that solves that problem.

Thanks for the information, I've always wondered how the router handles that. And by the way, if someone does happen to know what is wrong with StarCraft, I would greatly appreciate it if you informed me via a PM or a reply.

VBStrider
Quote:Original post by VBStrider

That's not what I meant... I mean if two people are playing the same game on the same LAN. So for instance, lets say a group of people want to play World of Warcraft together over a single LAN. They are not connected to each other, they are connected to a remote server.

After reading http://www.mindcontrol.org/~hplus/nat-punch.html it appears as though the router uses one of it's own ports, so that solves that problem.

VBStrider


The two computers on the LAN may choose the same outgoing port, but the NAT creates two different external ports. So even though the IP for both connections is the same (Router's IP) the external ports are different, because NAT chooses the external ports for you. So the server still sees a unique IP:port for each connection.

This topic is closed to new replies.

Advertisement