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

| | August 5, 2015

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?


One Response to “What is the best way to send/receive data about user coordinates and other properties?”

  1. Alexandre Desbiens on November 30, -0001 @ 12:00 AM


    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.

Leave a Reply