A simple Client — Server game.

Rohit Ramkumar
2 min readSep 25, 2016

--

This article describes a step by step implementation of a client-server(+ client) game.

Background

This story starts in a classroom at Syracuse University. Three friends are discussing the final project for the Android development course (I am obviously one of them). There are several topics discussed — chat apps, movie apps, games etc. Finally they decide to make an offline game. A game that can be played on an airplane especially when the three of them are sitting apart (and tolerating crying babies).Let me introduce you to Game of Cards (title inspired from the popular show).

Little about the game…

This game has no rules. The rules are decided and followed by the players. There are basic options available like the number of cards to be distributed, show/hide cards, play multiple cards, fold cards. These options will let you play any type of game (most of the popular ones).

Let’s talk technical…

Alright lets talk technical. The game works over a local hotspot that is created by the one of the players(server) and all the players(clients) need to connect to the hotspot outside the application. The architecture is pretty old fashioned and simple.

Server responsibilities:

  • Be aware of the current state of the game: It always maintains the current instance of the game.
  • Make others aware of the changes to the state: The server is responsible to make others aware of the game states on every update made by a player.

Client Responsibilities:

  • Update server on every move

Let’s look at some code…

Server-client was written using java sockets. One device has the server (& client) running and others have clients. Data is passed in the form of serialized objects. These objects have all the information needed to recreate an instance of the game.

Connecting

serverSocket = new ServerSocket(SocketServerPORT);
while (true) {
Socket socket = serverSocket.accept();
Thread socketListenThread = new Thread(new ServerListenerThread(socket));
socketListenThread.start();
new ServerSenderThread(socket, GAME_NAME)start();
}

Server maintains one listener and sender thread per client as the socket for every client are different. These threads take care of listening and sending . Similarly we have two threads running on every client for the same purpose.

Listening for game data

inputStream = hostThreadSocket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
Bundle data = new Bundle();
gameObject = objectInputStream.readObject();
if (gameObject instanceof PlayerInfo) {
data.putSerializable(DATA_KEY, (PlayerInfo) gameObject);
data.putInt(ACTION_KEY, PLAYER_LIST_UPDATE);
} else {
data.putSerializable(Constants.DATA_KEY, (Game) gameObject);
}
Message msg = new Message();
msg.setData(data);
serverHandler.sendMessage(msg);

The server receives a GameObject and distributes it to all the clients. When the clients receive this GameObject they simply refresh their states.

Sending game data

outputStream = hostThreadSocket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(message);

The GameObject is written to the output stream in order to be sent.

There are lots of ways in which all these functionalities could be accomplished. BLEs, Play Game Services(internet required) are some of the other options.

Acknowledgement

I would like to thank my other two partners, Kunal Shrivastava and Shivank Malik for helping me figure this project out.

Github: https://github.com/rohitramkumar308/GameOfCards

Google Play Store: https://play.google.com/store/apps/details?id=srk.syracuse.gameofcards

--

--