How to do simple local network games?

Hi everyone, I’m just now getting into learning network stuff such as sockets etc., and my goal has been to make fun simple little games like pong and lightcycles that play over a network. I don’t know much about networking but I assumed that there would be a way to have one application open on say, a computer at school, that listens for any other instances of it on the network and when the other opens up they connect and play… I’ve already managed to make pong that will connect if I open up two copies of the game on one computer and it works, but not on two computers side by side. After looking at it I realized it must be because the sockets are using “localhost”, which comes straight out of the examples that got me started.

Is it possible to have simple games that just hop onto a local network, like at school, or does it have to be more complex?

If it is possible, how do I find the host name to use?

Thanks for your patience guys, I’m new and there doesn’t seem to be a lot of help online for this as far as I’ve searched around :-\

You can send a UDP broadcast. This sends out a UDP message to all computers on a subnet. Typically, it goes like this:

  1. The server is listening on a UDP port.
  2. The client sends out UDP broadcast and then waits X seconds for a response.
  3. The server responds to the UDP message with another UDP message.
  4. If the client gets a UDP message within the timeout, it assumes it came from a server. The message received has the IP of the server, and the client can try to connect via TCP, etc.

A couple notes:

  • The broadcast will be blocked by most network hardware. It is really only useful for discovering servers running on the LAN.

  • UDP messages can get lost, so the client might want to send a few broadcasts.

  • You can see the UDP broadcast code for the client side here:
    http://code.google.com/p/kryonet/source/browse/trunk/kryonet/src/com/esotericsoftware/kryonet/Client.java#408

  • You can simplify your life by requiring the user to input an IP for the client to connect to. Find the IP address of the server (eg by going to it and typing “ipconfig”) and use that for the client.

  • For running a server visible to the internet, you can use a service like DynDNS to give your server a name instead of needing to remember the IP. You’ll need to configure any routers that your server is behind. Setup the router so that communication on the ports your server uses goes to your server machine.

Thanks so much, this is really helpful!

Whatever you do, dont try to do all your game traffic on multi-cast. If you use multi-cast for all your game traffic then two sessions of the same game on the same subnet will conflict.

I have a chapter all about this in “Practical Java game Programming.” It covers both discovery and communication and includes example code.

http://www.amazon.com/Practical-Programming-Charles-River-Development/dp/1584503262/ref=sr_1_1?ie=UTF8&s=books&qid=1306001203&sr=8-1