title: How to determine if I received entire message from recv() calls
body: <p>I am practicing on socket programming, trying to send a http request and then receive some <code>SOCK_STREAM</code> data from a server. This question is about the receive process.</p>
  
  <p><strong>Approach 1</strong>: my current approach simply use a while loop</p>
  
  <pre><code>while((received = recv(sockfd, response, BUFSIZ, 0)) &gt; 0) {
          //process response
  }
  
  if(received &lt; 0) {
          perror("Error receiving data!\n");
  }
  </code></pre>
  
  <p>basically, make <code>recv()</code> calls until it finishes/closes (which returns 0 as ) or it hits an error. </p>
  
  <p>However, I rarely see people doing this, most people on SO are suggesting of using length indicator, i.e., <a href="https://stackoverflow.com/questions/2862071/how-large-should-my-recv-buffer-be-when-calling-recv-in-the-socket-library">here</a> and <a href="https://stackoverflow.com/questions/1386142/handling-partial-return-from-recv-tcp-in-c">here</a>. Makes wonder if I missed anything with the above approach.</p>
  
  <p><strong>Approach 2</strong>: length indicator approach</p>
  
  <p>Okay, regarding the most preferred length indicator approach, how to get the total length of the entire recv message? </p>
  
  <p>Giving example as below</p>
  
  <p><a href="https://i.stack.imgur.com/Ub6ef.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ub6ef.jpg" alt="enter image description here"></a></p>
  
  <p>It's a HTTP header I retrieved, there's a field called <code>Content-Length</code>. Is it the length of the entire message (size of header + body = 468)?</p>