title: Writing a stream protocol: Message size field or Message delimiter?
body: <p>I am about to write a message protocol going over a TCP stream.  The receiver needs to know where the message boundaries are.  </p>
  
  <p>I can either send 1) fixed length messages, 2) size fields so the receiver knows how big the message is, or 3) a unique message terminator (I guess this can't be used anywhere else in the message).  </p>
  
  <p>I won't use #1 for efficiency reasons.</p>
  
  <p>I like #2 but is it possible for the stream to get out of sync?</p>
  
  <p>I don't like idea #3 because it means receiver can't know the size of the message ahead of time and also requires that the terminator doesn't appear elsewhere in the message.</p>
  
  <p>With #2, if it's possible to get out of sync, can I add a terminator or am I guaranteed to never get out of sync as long as the sender program is correct in what it sends?  Is it necessary to do #2 <em>AND</em> #3?</p>
  
  <p>Please let me know.</p>
  
  <p>Thanks,
  jbu</p>