title: C++ sending image but seemed failed
body: <p>I am implementing a Web server to send images to a client. When the client browser sends requests for images for the html page, the server intends to send the requested image back, or so it should have been doing.
  This is my code of sending the HTTP response. fd is the source file descriptor, cfd is the client socket descriptor, all fine without notable errors.</p>
  
  <pre><code>::send(cfd, firstLine.c_str(), firstLine.length(),0);  // HTTP/1.1 200 OK
  response.addHeaderDate();  // Date: xxxxxxxxx
  int fd = response.ffd;  // file descriptor
  // measure file size
  int file_size;
  file_size = lseek(fd, 0, SEEK_END);
  response.addHeaderLength(file_size);  // Content-Length: xxx
  lseek(fd, 0, SEEK_SET);
  // headers
  for (auto &amp;item : response.headers) {
      const std::string &amp;key = item.first, &amp;val = item.second;
      ::send(cfd, key.c_str(), key.length(), 0);
      ::send(cfd, ": ", 2, 0);
      ::send(cfd, val.c_str(), val.length(), 0);
      ::send(cfd, "\r\n", 2, 0);
  }
  ::send(cfd, "\r\n", 2, 0);   // blank line after headers
  char buf[4096];
  while (true) {
      int len = read(fd, buf, 4096);   // read from target file
      if (len &lt;= 0) {
          break;
      }
      write(cfd, buf, len);   // write to socket
  }
  close(fd);   // close file
  </code></pre>
  
  <p>I let the image to be also written to a local file, and the png image displays just fine in gnome's picture explorer.
  I turned on my little server and test it with Chrome. It shows this:
  <a href="https://i.stack.imgur.com/V0Rqt.png" rel="nofollow noreferrer">browser screen</a>
  Chrome shows a broken image icon at the place where I put my img tag. The html file is like this:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  testtest....
  &lt;img src="/assets/1.png"&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I turned to the developper tool of chrome. Checked Content-Length field in headers, which says 16996, the exact number of bytes of the image.
  Turned to a yet more powerful tool wireshark. It says file data is 16996 bytes, with an 'Expert Info' saying:</p>
  
  <pre><code>[Expert Info (Note/Malformed): HTTP body subdissector failed, trying heuristic subdissector]
      [HTTP body subdissector failed, trying heuristic subdissector]
      [Severity level: Note]
      [Group: Malformed]
  </code></pre>
  
  <p>This is also found in the http package for the html file. I believe it states the root of the problem since the message is not shown in other http packs, while I do not quite understand it. I guessed it was about I did not send '\0' when the package is fully transmitted. I tried this and still does not work.
  I am new to servers and network programming. Thanks for reading and please help. This is really a pain in the ass.</p>