I am practicing on socket programming, trying to send a http request and then receive some SOCK_STREAM
data from a server. This question is about the receive process.
Approach 1: my current approach simply use a while loop
while((received = recv(sockfd, response, BUFSIZ, 0)) > 0) {
//process response
}
if(received < 0) {
perror("Error receiving data!\n");
}
basically, make recv()
calls until it finishes/closes (which returns 0 as ) or it hits an error.
However, I rarely see people doing this, most people on SO are suggesting of using length indicator, i.e., here and here. Makes wonder if I missed anything with the above approach.
Approach 2: length indicator approach
Okay, regarding the most preferred length indicator approach, how to get the total length of the entire recv message?
Giving example as below
It's a HTTP header I retrieved, there's a field called Content-Length
. Is it the length of the entire message (size of header + body = 468)?