Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Even games that are 100% client/server sometimes have issues when the client is behind NAT. Peee-peer games are even a bigger issues. Some games need to use multiple transports (such as UDP and TCP) or multiple connections (such as a different UDP port for voice).

What are some ways to make sure a game works reliably when running behind a NAT router?

  • Peer-Peer: No centralized server exists. Player A starts a game and Player B wants to join
  • Client-Server: A centralized server on a well known address (hostname) accepts all incoming connections. Each client only communicates with that server.
  • Combo: Where the server is just matchmaking, but game updates are peer-peer. Different peers may see each player with a different IP/port potentially (e.g. some clients are behind the same NAT and some are on a different router)
share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

The technique most commonly used is referred to as NAT punch-through. Here's a decent intro: http://www.mindcontrol.org/~hplus/nat-punch.html

There's an OSS project at least for UDP: http://udt.sourceforge.net/index.html

RakNet supports punch-through, too, I believe. It's commercial, but has a free "Indie" license. See here: http://www.jenkinssoftware.com/

Googling for "NAT punch" will get you plenty more reading material

share|improve this answer
add comment
  • Don't have a client inform the server or peer of its own address. In other words, don't embed client A's IP in a game packet and have client B or the server reply to that address. Always reply to the address/port that the message originated from.

  • Minimize the different UDP or TCP ports used for the game. This makes setting up NAT rules easier.

  • Allow the user to override the default port used in the game. This way several servers can be hosted behind the same NAT.

  • Document the ports and any rules they follow in your game documentation (thanks for that one Zorba). In Descent 3 I went as far as creating a stand alone test application that would help you troubleshoot your router or NAT configuration by sending packets to a test server.
share|improve this answer
5  
In addition to this otherwise-good reply, include a note of the game ports in the game documentation. It's amazing how many games don't do this - people root out the ports manually with "netstat" and get half of them wrong. –  ZorbaTHut Jul 17 '10 at 14:14
add comment

Along with other answers, use an existing wheel : http://miniupnp.free.fr/ This library battles almost all of the annoyances in function form , giving you control over when and how to punch and pull :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.