Take the 2-minute tour ×

We are using JSON right now and want to move to a binary format for some types of messages between client and server.

Should I just read structs into the socket? Use proticol buffers/thrift?

How should I represent arrays of data?

What should the interface look like for packing/unpacking data?

share|improve this question
add comment

2 Answers


  1. you are talking about converting to a buffer of bytes
  2. You are using UDP and performance is a concern

Try to avoid wasting space in your packet for defining structure. I.E. send, at minimum, a byte to denote the type of packet, then just assume each packet received follows the predefined structure for that type of packet


  • Yes, read the whole struct IF you NEED the whole struct
  • No, make the packet structure yourself, This will surely be smaller than serialization using these methods; you should know exactly what data the packet should include

How should I represent arrays of data?

  • As arrays of data. When receiving continue reading the buffer until end of data to avoid sending a Count of the array's elements

What should the interface look like for packing/unpacking data?

  • You could easily setup a bunch of methods to convert basic types to bytes, from there build on these methods to convert custom types as well. The specifics on how to do this could be found almost anywhere I'm sure (I use C# personally)

One last thing, packet size is a concern, especially for a snapshot: size = packetSize x entities x connectedPlayers; So you might have 60 x 10 x 16 = 9,600 bytes per packet Then sending this 20 times a second: = 192,000 bps = 187 KBps. This is obviously a high bandwith upload speed. Thus the need to minimize each of the factors contributing to packet size where possible.

This article has helped me immensely: Valve Multiplayer Networking

share|improve this answer
Another article that I discovered while reading various object serialisation and networking questions here a few weeks ago was this one which describes how the Unreal engine does it. A good point of comparison for the Valve source. –  Martin Foot Aug 31 '11 at 8:42
Your array method is not going to work in the general case - where is the 'end of data'? Even if your messages are delimited, it would mean you can't have more than 1 array per struct. To fix this, the original poster could either stick to fixed length arrays, or ensure there's only 1 array per struct (at the end of the struct), or send a count value at the start of the array. –  Kylotan Aug 31 '11 at 10:48
Just one more tip: Remember to treat endianess, this can be very anoying if you don't know that such thing exist. –  user9471 Aug 31 '11 at 13:03
Good point @Kylotan , agree that in certain cases this extra data can't be avoided; but if I find myself adding multiple arrays to a single packet, I would consider sending multiple packets instead –  indeed005 Sep 1 '11 at 0:47
add comment

This problem has been solved by Google and Facebook:

  1. Google's Protocol Buffers — Google is a big user of C++:


  2. Apache Thrift (formerly by Facebook):


share|improve this answer
Google's Protocol buffers are too slow for larger scale, real time games. I did find them quite nice for prototyping and smaller player numbers because of the versioning, however. As usual, your profiler will tell the real story. –  Patrick Hughes Sep 2 '11 at 17:48
Well, they're good enough for Google, and Google scales pretty well, and they worked well when I used them. That's why I recommended them. –  a paid nerd Sep 2 '11 at 20:57
Google doesn't require real time performance. Google does require reliability and up-time, both of which are well served by Protocol Buffers. The complexity of all that fallback versioning and boilerplate code generation adds overhead and when you're sending and receiving 1000 updates at 50-100msec intervals it adds up. Profile a Protocol buffer several versions old against a coded serializer specific to the data at hand. @indeed005 has the gist of it. –  Patrick Hughes Sep 2 '11 at 21:22
+1, because although these formats are a bit too large and slow for most real-time or high-bandwidth games (due to containing extra information that allows you to reconstruct arbitrarily complex packets), that's not to say they aren't useful in some games, eg. turn-based ones. If optimising every resource isn't necessary then these formats can save you a lot of time, and they're certainly more efficient than JSON. –  Kylotan Sep 4 '11 at 12:27
add comment

Your Answer


