Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Note: This is a question about networking, not gaming. I'm using StarCraft merely as an example.

The game StarCraft (not StarCraft 2) supports online play. One person hosts, and other people join. If the host leaves during the game, the game can continue indefinitely without the host. How does that work?

Consider the following scenario:

  • I host a 3 versus 3. Note that, for people to be able to join, my router has to be configured to port forward 6112, TCP and UDP (see Blizzard Support).
  • Five people join. Everyone (myself included) is behind a router. I start the game.
  • Three minutes into the game, about twenty zealots pour into my base. Nobody helps me.
  • My base is eliminated, so I leave (the game doesn't give you a choice).
  • The game continues without the host (the home team has two players left).

How do the remaining five clients (who didn't need 6112 forwarded) remain connected?

If I understand correctly: if two programs want to talk to each other, one of them has to be a "server" and listen for connections on a port, while the other has to be a "client" and initiate a connection request on that port. They can't simply start sending packets to each other (and I don't even know how they would, if both are behind routers).

Someone hosting a game is a server, while those joining it are clients. It's easy to see how the clients can start communicating with the server. What I don't get is: how do the clients start communicating with each other without going through the server? Does the Internet Protocol allow a server to initiate connections between clients?

It's entirely possible that, in the case of StarCraft, game traffic goes through Battle.net servers. StarCraft does maintain a connection to Battle.net during games (for messages from friends, etc.). However, I doubt game traffic goes through it, because if it did, why would hosts need to port forward 6112?

My question is: can a server, with multiple clients connected to it, initiate connections between them?

share|improve this question
2  
I'm liking the Starcraft reference here... –  Jon Jul 26 '11 at 3:30
    
I host a 3 versus 3. Note that, for people to be able to join, my router has to be configured to port forward 6112, TCP and UDP (see Blizzard Support). Apparently this is needed to start a game so users can find you, but not required to remain connected. –  JeffO Jul 26 '11 at 13:09
    
@Jeff O: Battle.net servers maintain a list of available games. However, if the host doesn't port-forward, it will simply hang when others try to join (I think). –  Joey Adams Jul 26 '11 at 13:49
add comment

2 Answers

up vote 18 down vote accepted

That sounds like UDP hole punching

Let A and B be the two hosts, each in its own private network; N1 and N2 are the two NAT devices; S is a public server with a well-known globally reachable IP address.

  1. A and B each begin a UDP conversation with S; the NAT devices N1 and N2 create UDP translation states and assign temporary external port numbers
  2. S relays these port numbers back to A and B
  3. A and B contact each others' NAT devices directly on the translated ports; the NAT devices use the previously created translation states and send the packets to A and B

In this example, you are S. Your opponents are A and B. When you are kicked from the game, your opponents can continue playing because they had negotiated a connection to each other when they first connected to you.

share|improve this answer
    
Wow +1, I had no idea NAT could be used like that –  TheLQ Jul 26 '11 at 6:09
    
The problem with NAT's is that you need to start from behind the firewall. Hence you need at least the initial computer to be publicly available or there is nothing to connect to. –  user1249 Jul 26 '11 at 13:16
add comment

If two programs want to talk to each other, one does not need to be a server and the other a client. That's one model of networking, but not the only one. Another very common model is the peer-to-peer model, where everyone is both a client and a server and anyone can talk to anyone. I don't know too much specifically about Starcraft multiplayer networking internals, but I'd guess that it most likely accomplishes what you described by using a peer-to-peer model.

share|improve this answer
    
P2P models still generally require ports to be forwarded by at least one person in each pair of peers to make the intial connection. Try downloading a torrent with and without ports forwarded and see the difference in the number of connections. –  Snorbuckle Jul 26 '11 at 8:00
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.