Question

For the past few days I've been working on an online game, a 2-player game where one starts a server (listens on a certain port) and another one connects to him by entering his IP. There are two problems with this method:

  • It is very uncomfortable to input the opponents IP.
  • The server must not be behind a router, because the NAT prevents it from working.

The first problem can be solved with a matchmaking service that takes care of the IP addressed without the users' concern. But I am not sure how I can solve the second problem. I have read about "TCP hole punching", but according to what I have read, it is not possible to perform this when both players are behind a router. If that is true, then how do games with matchmaking services like Halo 3, where one of the players is the host, work?

Thanks in advance.

Était-ce utile?

La solution

You could use a technique called UDP Hole Punching, for gaming UDP might be the better choice anyway. But you still need some sort of rendezvous server to enable the initial handshake of the clients.

See here for general information

Autres conseils

I believe your second problem can also be solved by your matchmaking service/server. That server should be able to know the public IPs and ports of the 2 players behind their respective NATs. The packets that the server will receive will contain the public IP of each player. All the server has to do is to forward a player's public IP+port to the other one. When each player knows the public IP of the other one, they should then be able to talk to each other directly. However, the success of NAT traversal techniques highly depends on the configuration of each NAT device.

In VOIP/SIP land, they have a couple of RFCs that deal with this. Check ICE and ICE-TCP (for TCP) which make use of STUN and TURN. It's probably a bit overkill if you implement these protocols in your game but I think they're a good resource for learning the techniques in NAT traversal.

This question on GameDev.SE has several useful answers, including a link to this introduction to NAT punch-through: http://www.mindcontrol.org/~hplus/nat-punch.html

You shouldn't write a matchmaking service that is responsible just for finding a match (AKA server list).

A better solution would be to create a main server that players can join to automatically. The main server allows to create or join existing matches. Players shouldn't connect to each other, they should always connect to the main server.

For example, Halo's "main server" is Xbox Live.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top