title: How to use "Connection: close" in HTTP request or separate header and body? C
body: <p>I want to download a image through socket in C. In my code I have a fuction that return the Content Length of the body-content. How can I receive amount of content_length data after "\r\n\r\n" in a loop and save it to file? I've heard about "Connection: close" how can I use it?</p>
  
  <pre><code>// Generate HTTP request
  sprintf(send_buf, "%s %s %s%s %s%s", "GET", path, "HTTP/1.1\r\n", 
                                  "HOST:", hostname, "\r\n\r\n");
  
  send_len = strlen(send_buf); //length of request to send
  bytes_send = send(sockfd, send_buf, send_len, 0);
  
  if (bytes_send &lt;= 0)
  {
      perror("nothing sended\n");
      exit(1);
  }
  
  printf("Bytes send: %d\n\n", bytes_send);
  
  // receive data
  
  
  FILE *fd = fopen("img.txt", "wb");
  
  bytes_recv = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
  
  /*char *img = strstr(recv_buf, "\r\n\r\n");
  img += 4;*/
  
  int content_length = content(recv_buf);
  
  //how can I recv() data with length content_length in a loop after "\r\n\r\n"?
  
  fwrite(recv_buf, 1, bytes_recv, fd);
  </code></pre>
  
  <p>after writing this if I remove header from img.txt and change the file to img.jpg the image seems to be corrupted because not all of body-content is received.</p>