I've got about a year in Java experience, and would like to set up a server and client for an applet game. However, there doesn't appear to be any tutorials out there on anything like I want to use.

I would the server to be able to store an array of x and y coordinates with a player name somehow associated to them, and send them to multiple clients in a short time span. I would like the client implemented in the applet, and be able to request any player's position data.

I'd like to use UDP, because it seems to be the best option for efficient (if less reliable) transmission of data.

If anyone could give me some pointers on how to do such a project, or point me to an appropriate tutorial, I'd certainly appreciate it.


The problem in network programming is, that everything you want to send has to be serializable in some kind. So if you have different objects with attributes you have to collect all data in a way you can send them via UDP

So if you have a ball and a box with some kind of coodinates

ball: x:10 y:20 box: x:50 y:50

you should make them parseable that your server can parse them back.

A way of doing this could be putting everything in a string

"ball{x:10;y:20};box{x:50;y:50}" or generating some kind of valid XML (but XML has a lot of overhead just for a nice format)

How to implement a UDP Server/Client you could use the examplecode from http://systembash.com/content/a-simple-java-udp-server-and-udp-client/

You want to use UDP so you cant be sure that all of your packets will reach the clients. Therefore you sould implement some kind of timestep synchronisation, that the server could use to simulate the movements of the players even if he didn't receive any data from a player.

To do this the Server counts its own time and sends it to the clients when they login. From there on the clients count their own time relative to the servers time and sends their relative steps with the data to the server. So the server "knows" what client didn't sent data in the current timeframe (or what data just didn't got to the server)


I actually like Aron_dc's answer, but i thought I would add an answer to provide additional things to consider.

1) UDP vs TCP - You have to choose a particular protocol and understand the cost and benefits. Picking UDP is quicker, but you have to create your own control code on top of UDP, common problems with UDP : packet loss, firewalls, packets out of order and knowing when the client has disconnected (using timers and "are you alive" packets). With UDP you may have to think about storing packets, numbering them and resending them if they don't get acknowledge. It maybe a bit of a headache for the first time.

2) Multiplayer component design:

  • State machine : how many states the client may have : connecting, playing, disconnecting, changing level?
  • Components : What are you sending to the client and to the server? Login information, inventory, movement, chat.
  • Acks/Nacks : If something happens in the game : like a player dies or wall get destroyed, does every client have to say "yeah i got that information", because if the don't the game state will desynchronize and everyone will be playing their version of the game. (PS: I played a game once with bad multiplayer code and it was kind of funny to know that while I was winning the game on my machine, it was not the case on the other player's machine.)

3) Serialization

You need to have a good handle on understand serialization, as it will also help you write save/load game functionality as well. Essentially it is taking your objects and storing them in a different format. So for example you want to serialize movement message.

MovementMessage(int x, int y, int z);


byteBuffer.write(MOVEMENT_MESSAGE_TYPE); // a unique number that lets you know it is movement message


messageType = byteBuffer.readInt();

switch(messageType) {
       msg = new MovementMessage()
       msg.x = byteBuffer.getInt();
       msg.y = byteBuffer.getInt();
       msg.z = byteBuffer.getInt();

If you want to send multiple messages in one packet you may add additional details to your protocol, for example

[how many messages] [first message] [second message]
02 01 1 2 3 01 2 3 4
two messages, first message is of type 01 with data 1,2,3 and second is also type 01 with data 1,2,3

4) Library:

There are two libraries that I am thinking of right off the bat : Google Proto Buffers and Java Netty. I have had a lot of experience with java netty and use it in my projects. It has example for clients and servers. There are probably more.

5) Examples:

When I do multiplayer research, I usually try to find out how the other guys did it. I look up information on Quake, Unreal and other popular titles and read up how they designed their multiplayer component. Once you can understand the design the rest is just picking the right code to do it.


Not the answer you're looking for? Browse other questions tagged or ask your own question.