title: How to implement binary TCP protocol
body: <p>I created a number of TCP client / server applications in past, that were always using very simple text based protocols, either text messages separated by new line or Xml streams.</p>
  
  <p>Now I am creating a simple protocol for a game server that would mostly exchange Vector information and handle some simple RPC calls with clients. Because it's going to be a game server I really want to it to be super fast and lightweight. For this reason I decided to implement it as a binary protocol (see <a href="https://stackoverflow.com/questions/2525188/are-binary-protocols-dead">Are binary protocols dead?</a> for some more information on what I mean).</p>
  
  <p>I have a rough idea how to do that, but before I start working on it I would like to confirm that this approach is actually going to work and if there isn't any better, commonly used one. I never implemented binary TCP protocols in past.</p>
  
  <p>I am going to send all information in "packets of information" which I just call "datagrams". Another important constant within the protocol is the size of floating point numbers in bytes, which I just call <code>BLOCK_SIZE</code>. I am going to use multiple languages (client will be C#, while server is C++) and I need to make sure that all platforms (x86, x64) will have same sized numbers within datagrams.</p>
  
  <p>First information that server sends to client is going to be a single byte (<code>sizeof(char)</code>) containing the value of <code>BLOCK_SIZE</code> just to make sure that floating point numbers (I am using <code>double</code> because it's big enough for my purposes) are going to take same amount of bytes on both server and client. Then a stream of "datagrams" follows until end of communication channel.</p>
  
  <p>Layout of datagram is:</p>
  
  <pre><code>Size in bytes / name                  Description
  BLOCK_SIZE / type            Type of datagram (for internal purposes I need to figure out what I am actually going to process - I could probably create header instead that would describe it in details, but for my purposes one `double` can contain all possible types I will use)
  unsigned BLOCK_SIZE / size   Length of datagram - number of bytes that will follow. I just hope I will never need to send a single datagram that would be bigger tham maximum value of unsigned double :P
  size / data                  Data that are contained within datagram, I will process them based on what `type` they will be.
  </code></pre>
  
  <p>I suppose that for receiving I would just always create a buffer big as <code>size</code> announced by either side would be and keep pushing data into it, unless I get them all. And then I can start processing another datagram.</p>
  
  <p>I believe this should work as long as client and server always send correct data with not a single byte being wrong, extra or missing. I don't know if TCP is reliable enough, or if I need to implement some error checking as well. I have no idea how.</p>
  
  <p>I only need to transfer huge amount of arrays of floating point numbers, eventually some very simple strings, but I don't really need to care about that right away. Is this a right approach or should I do it in a different way?</p>