Sponsored By

I'm tackling the meat of the behind-the-scenes code for my centipede game. This means sending and receiving commands through sockets and making sure exceptions are handled if one or both machines happens to die.

Jack Matthewson, Blogger

February 10, 2013

3 Min Read

I have made so much progress over the weekend. All told I've probably put a solid 10 hours of programming time in, as well as work on other coursework. I don't know if this is because I have a demonstration date looming, or because I feel like I'm reaching a point in my code where I can see everything coming together and it feels less like a load of separate mini projects and one cohesive whole. Currently I'm running a server on my main PC and the client software (which includes the centipede game I posted up in my previous post) on my netbook. This means coding on two separate devices which I actually enjoy. It's easier to sort the code physically between two devices rather than one device with two separate projects. 

I'll try and break down the behavior of my server code so far.

  1. Server: Opens a socket on port 7777

  2. Client: User inputs the IP address of the server and port number 7777 then presses connect. Client connects to server. (See image below)

  3. Server: picks up the incoming connection, prints out the IP address of the new client and launches a ConnectionHandler thread to handle all further interactions with this client.*

  4. Client: Requests the ID of the thread that is controlling it.

  5. Server: Fulfills the client's request for thread ID.

  6. Client: If the game has not started yet, it will continually ask the server if it can start.

  7. Server: As soon as the end user gives the go-ahead, the server acknowledges the client's next request to start with "true" instead of "false".

  8. Client: Picks up the signal to start the game and immidiatly requests the location of which screen edges are walls and which lead on to other systems.

  9. Server: Sends this information in a simple comma separated string.

  10. Client: Pareses the wall information and runs a method to set up the walls correctly. 

  11. Client: Asks the server if it can spawn a centipede.

  12. Server: when required, sends the client a message to spawn a centipede, as well as the location where the centipede should be created.

  13. Client: Dutifully spawns the centipede, then waits for it to dissepear off a screen edge. Informs the server when this happens. Step 11 starts again.

*These ConnectionHandlers are all stored in a LinkedList of connections. There is a daemon thread that continually monitors this list for dropped or closed connections. If it finds one, it deletes that entry from the list to free up space.

Yay Swing

Client Software


That's it in a nutshell. There's exception handlers in place which gracefully closes the server's connection if the clients is manually closed, or switches the game into offline mode if the server's connection to the client is dropped.

The next step is to create an interface for the server that can graphically represent each of the connected clients and position them in a grid. Once this grid is formed, the server should be able to generate the wall layout for each different client and broadcast this information when the game is started. My next post here will be when I have this working. Perhaps as early as tuesday. 

Read more about:

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like