I have never programmed a client / server before but I would like to create a game where objects can be passed back and forth between two players who reside on separate machines. An example object would be a game piece and the reason it would be passed is because the game piece changed its position on the board.

Both players will be sending objects across the network to one another so that both player's game boards are updated (think Monopoly or chess).

How do I begin to code this? So far I've been able to create a server socket and listen for a connection and I can successfully connect to that socket with a client socket. What do I need to do now?

5 Years
Discussion Span
Last Post by JamesCherrill

This is a complex problem that you will need to split into simpler parts with attainable tasks.

Are you successfully send a message to the server and receive a response?

Think about what your messages will look like when they are sent to the server, and received by the client - what is the structure of the data?

How will you represent the "board" and position of the "pieces" on the board on the server? In an array? a map? a database?

How will you represent the same information on the client, not graphically, but in memory.

How will you graphically present the state of the board to the client? (I recommend you start simply with console output to the clients.)

From there, you can start thinking of things like winning conditions, error conditions, illegal inputs, and all the other things that go into creating a game.

Sorry I don't have any code to give you, but I'm sure you can find plenty of examples on google. These are merely the starting points of the thought process that has to go into your somewhat challenging project.

Hope this helps.


I agree it does need to be split into simpler tasks. I'd like to start with the client and server aspect.

I have looked at a few examples of the implementation but I am still confused. Most examples create a server by listening on a port and then entering a loop that looks for input from the socket. This is simple and understandable but it doesn't really facilitate the exchanging of messages between players. Do I need to somehow mimic this behavior on both machines?

I have also seen an example where the server creates a new thread that constantly loops looking for input/output from an Object stream. Does this type of implementation need to occur on both machines?


It's enough to have the server listen and the client make a connection to it. Both ends can then open input and output streams over the connection so messages will flow both ways. Both client and server need to have a Thread that loops blocked listening on its input stream so it will be able to process any inbound messages as and when they arrive. Once the connection is established (from client to server) the rest is identical and symmetrical between the client and server.

Object input/output streams are a pretty good normal choice because you can send String Objects to act as message identifiers, followed by Piece or Board or Move or whatever Objects the application logic requires.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.