How to make your PC work as a server for your game to the world

jwatte's picture
So, you have written a game and a server, and now you want the rest of the world to be able to play the game on your server?

The rest of the world needs to get an IP address that maps to your machine in order to communicate with your machine. This IP address is generally assigned by your ISP (Comcast, Verizon, whoever is your internet provider.) Thay may also decide to change this IP address at any time without telling you, unless you pay them for a "static IP service," which in turn generally needs a "business internet" plan rather than a "consumer internet" plan. Additionally, the IP they issue will generally point at your modem or router/firewall, rather than your PC. Thus, you have to set up port forwarding from the firewall to your PC for the port that your game listens on, for the packets to actually get to your PC.

The mapping from "somedomain.com" to the actual IP address is done through the global DNS system. To insert your own name into this system, you typically pay a registrar some money per year to matain this mapping. Expect to pay between $8/year (for the ultra-cheap, crappy registrars like GoDaddy) to $50/year or more (for the value-added, high-service options.) Personally, I use Dreamhost, for $10/year, but you also have to have a hosting account with them for that to make sense. The problem here is that the name -> IP mapping can't change very quickly, so if your ISP changes your IP address (because you didn't pay for a static address,) your players will get "could not connect" errors, or maybe someone else spins up an evil server on the IP that you used to use...

There exists a way to map from a static name to a dynamic IP; "dynamic DNS." The most well-known service is probably DynDNS.org. The way it works is that you set up your PC or firewall to update DynDNS.org whenever your IP changes. This maps a name called something like "yourgame.dyndns.org" to an IP address. You then set up a name redirect for your "real" domain -- "yourgame.com" -- to the "yourgame.dyndns.org" name. This is done with a CNAME record, and most good domain registrars should let you set one of these up.

With all of these machinations, you can get "yourgame.com" to map to your PC, and it needn't cost you more than $10/year. However, there are several draw-backs to this set-up:

  • If you accidentally kick the power cord of your PC, it will kill the game server. Or your cat/spouse/cleaning-lady/power-company/whatever.
  • If somebody doesn't like you or one of your players, they may decide to start a DDoS against you by renting a botnet for a few hours. This may make your ISP disconnect your consumer Internet service, and will certainly take down your game.
  • If you need to use the PC for something else, then you have to disconnect current players.
  • Some of the service providers involved (like your ISP) may have policies against this, or may suddenly change their services, and you'll be unable to stay online.

If you want to run your game as a serious undertaking, the best thing to do is to pay for a hosted, or co-located, or virtual private server. This costs from $20/month and up, depending on bandwidth and CPU/RAM/storage needed. These machines run in well-managed data centers, and can generally have a static IP that you can map yourgame.com to. Providers include aws.amazon.com Elastic Compute Cloud, serverbeach.com, linode.net, interserver.net, 1and1.com, dreamhost.com, and many others.

Note that you want a "hosted server" of some sort, not a "web server." Also, if your server is written for Windows, expect to pay more per month, to cover the Windows license, and not all service providers support Windows, so make sure to check for that!