# How to handle network game [closed]

I'm currently building my own game using LIBGdx in JAVA but i'm facing some trouble. My game is base like this :

A player have a position and some other parameters that are float, int types. He also have a class name Launcher that contains 3 array list and every update of my game it sends way to much datas and it's making the game very laggy.

How my system work :

Serveur launch and is waiting for connection in another thread. Meanwhile the main thread of the server is updating the map. Suddenly a player connect to the server, it add a new PlayerGestion inside the server which allows me to receive update from this single player. Meanwhile the server keeps updating the maps with the players datas. My problem is that after every map update, when the server send datas to every players, it sends heavy datas that is the arraylist of players (containing the datas i listed below). So my question, is :

Is the way i'm doing a client / server good ?

Is the writeObject method good to use in a server / client

Is it possible to do an online by sending only the x / y players position ?

I'll be able to answer more question if you need to and show some line of code if it can help you help me.

Thanks !

## closed as unclear what you're asking by Anko, Philipp, Seth Battin, congusbongus, KromsterSep 16 '14 at 19:32

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Everything you've mentioned sounds OK to me. What specifically are are you stuck on? – Anko Sep 13 '14 at 19:44
• Here is my problem, this is how my connection is when the serv is started (i use the windows data monitor) : serv on and this is my data monitor when a player connect to the server : player on – Kamigaku Sep 13 '14 at 20:43

From my knowledge using the Serializable language feature for realtime game networking thats running at 40+ FPS is very bad.

I would send data in a plain binary stream for example Ive got to send the following variable in a class...

(String) player name, (Integer) player health, (Integer) player model id, (Integer) player x, (Integer) player y

I would send the player name as length and then name, so for 'John' I would write.

4John

Followed by the Integers (Limited to 9999) health 100, model id 1, x 30, y 30

health (100) 0100 model id (1) 0001 x (30) 0030 y (30) 0030

So the full message would be...

4John0100000100300030

Totalling 21 bytes which is massive reduction in data compared to serialization of objects, And I'm guessing that there is some overhead writing and reading objects anyway.

Then later when you need to update due to the player moving you send the new health (100), x (35) and y (30)

010000350030

• +1 for the string data passing as opposed to objects. Although it would require a bit of configuring to make it work, it would greatly reduce package size. – StrongJoshua Sep 14 '14 at 11:58

It depends a lot of how your game actually works, but in most games it is not necessary to send all positions of all objects after every frame. In most cases, most of the information will either not change right now or will be changing in very predictable ways (like an object moving in a straight line).

It is often a lot more bandwidth-economic to only inform the clients when things change and send nothing when things stay the same. So instead of sending the equivalent of

bob is standing at 12 45
bob is standing at 12 45
bob is standing at 12 45
bob is standing at 12 45
bob is walking at 13 45
bob is walking at 14 45
bob is walking at 15 45
bob is walking at 16 45
bob is standing at 16 45
bob is standing at 16 45
bob is standing at 16 45


you just send

bob is standing at 12 45
bob starts walking east with speed 1 tile per frame
bob stops walking at 16 45


Using Java ObjectOutputStream and ObjectInputStream to serialize your messages as objects is a quick, but dirty solution. The ObjectOutputStream will add a lot of meta-data you don't really need (like the fully qualified class-name for example), so it will use a lot more bandwidth than necessary. But it is really easy to implement a network protocol this way because the classes are able to exchange almost any object over-the-wire without requiring any additional code (the objects need to implement Serializable, but this is just a marker-interface without any methods, so adding it to any classes you want to exchange is trivial). When you want to develop a working prototype as fast as possible, you can use these classes as a stopgap measure, but you should be prepared to replace them with your own serialization and deserialization system to encode your messages in as few bytes as possible.

To your 3rd question, yes, you could preload the necessary models and just update positions and whenever a new players connects, load the extra info (you could do this asynchronously so as to prevent lag spikes).
You could also calculate movement cycles (animations) on the client or on the server and then send the player's current animation 'step' to the client (where the steps would also be defined). Of course, you can do the same for all other aspects of your game, this is just an example.

The rest seems fine.

*Note that I don't have personal experience in creating server-based games, but these are my observations from playing them.

• I have to handle more than players, i have ennemies and spells. So we can say that for players and ennemies, but then how do i handle spells ? I mean, when you play a game like League Of Legends or even Diablo, i could get that what the players send to the server is only their inputs (i use an array of 255 booleans who are all the inputs and i send it only when it's updated) then the server updates the spells to the right player but no are they answer send to the players to tell him : "you have use this spell, now it's on cooldown", and you see on your skill tab that your spell have cooldown. – Kamigaku Sep 14 '14 at 8:36
• @Kamigaku That's a similar situation to what I described. You handle all animation and whatnot on the client, then send to the server that you used Spell X (the server already knows your position) and the server then calculates how much damage is done to everyone. That information and the cooldown time is passed down in the next information pass. – StrongJoshua Sep 14 '14 at 11:56