Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

put on hold as off-topic by Anko, Josh Petrie 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about "how to get started," "what to learn next," or "which technology to use" are discussion-oriented questions which involve answers that are either based on opinion, or which are all equally valid. Those kinds of questions are outside the scope of this site. Visit our help center for more information." – Anko, Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

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)

share|improve this answer
add comment

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);

toByte(...)

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

fromBuffer(...)

messageType = byteBuffer.readInt();

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

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.

share|improve this answer
add comment

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