# What is the best way to send/receive data about user coordinates and other properties?

For example If I have 2 users, and each of them have an object, and that object has: x coordinates, y coordinates, angle, state, and other properties...

I use java

let'say the game changes at 30 frames per second, so each frame something happens, I should send A JSON object through sockets to my server, but the more data we have the bigger the JSON gets, I wonder if there is a way to optimize this sending of data and receiving, cause FPS games send much data, and does not lag, how the games LIKE Dota 2, Counter-Strike etc...send the data? do they use JSON to send the position, angle and other things to notify the server?

thanks

• JSON has the advantage that it is human-readable and human-writable which makes debugging netcode really easy. But with a binary protocol you can represent the same data with far fewer bytes. – Philipp Jun 11 '15 at 7:54
• You can try and compress your data before sending it (zip). – Vaillancourt Jun 11 '15 at 9:55
• What specifically is the problem here? Is JSON really too big? Have you profiled it? How much too big is it? – Anko Jun 12 '15 at 17:09
• possible duplicate of How to write a network game? – Seth Battin Jun 28 '15 at 17:59

## 1 Answer

JSON

Philipp makes a good point about JSON. It is human readable and makes debugging network code easy. If you have no experience in programming network code, this would be the way to go. Yes, there is a lot of overhead by using JSON, but for small to medium data transfers, it should be more than enough. And like Alexandre Vaillancourt said, you can always compress data before sending it.

Faster and smaller

But if you want to optimize your network code to send the less data as possible for your game to use the less bandwidth (and be fast), then I would suggest using frames.

Let's say you want to send all the information you mentionned:

x: int
y: int
angle: float
state: int


This is a player information packet. We need 16 bytes of data in the frame. For the server to be able to read that frame, we will need a header. Let's use 1 byte and say the player information packet is the frame with the id 1. You would then have a frame that looks like this:

Bytes  Information
0 1 (For player information packet)
1-4 X position (as an int)
5-8 Y position (as an int)
9-12 Angle (as a float)
13-16 State (as an int)


Wrap this in a UDP or TCP packet and send it. The server, when receiving it, would then read the first byte of data to extract the identifier, see that it's a player information packet, and know exactly how to read the remaining data.

About big games like Dota 2 and CS:GO

Don't even bother with this. They use highly sophisticated networking algorythms that are in almost no way applicable to small games people like you and I make. Keeping it simple is the key, because if you ever need to debug your packets (and trust me, you will, and a lot), you cannot call a team of 10 people specialized in network programming like Valve would do.