title: Handling multiple recv() calls and all possible scenarios
body: <p>I am fairly new to C and writing a TCP server, and was wondering how to handle <code>recv()</code>s from a client who will send commands that the server will respond to. For the sake of this question, let's just say header is 1st byte, command identifier is 2nd byte, and payload length is 3rd byte, followed by the payload (if any).</p>
  
  <p>What is the best way to <code>recv()</code> this data? I was thinking to call <code>recv()</code> to read in the first 3 bytes into the buffer, check to make sure header and command identifiers are valid, then check payload length and call <code>recv()</code> again with payload length as length and add this to the back of the aforementioned buffer. Reading Beej's networking article (particularly the section <a href="https://beej.us/guide/bgnet/html/#sonofdataencap" rel="nofollow noreferrer">Son of Data Encapsulation</a>)), however, he advises to use "an array big enough for two [max length] packets" to handle situations such as getting some of the next packet.</p>
  
  <p>What is the best way to handle these types of <code>recv()</code>s? Basic question, but I would like to implement it efficiently, handling all cases that can arise. Thanks in advance.</p>