title: HTTP response connection reset?
body: <p>I'm writing up a simple web server that implements select components of the http 1.1 protocol and thus far I have a tcp server that serves a static web page to all tcp connections it receives. I feel like there might be a race condition or some other event happening that I'm not aware of.</p>
  
  <p>Here's the essence of the code in question:</p>
  
  <pre><code>char hello[] =
      "HTTP/1.1 200 OK\r\n"
      // "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n"
      // "Content-Length: 131\r\n"
      // "Connection: close\r\n"
      "\r\n"
      "&lt;html&gt;\n"
      "&lt;head&gt;\n"
      "  &lt;title&gt;An Example Page&lt;/title&gt;\n"
      "&lt;/head&gt;\n"
      "&lt;body&gt;\n"
      "  Hello World, this is a very simple HTML document.\n"
      "&lt;/body&gt;\n"
      "&lt;/html&gt;\n";
  
  while(true) {
      addrlen = sizeof(peer);
      sockd2 = ::accept(_sockd, (sockaddr*)&amp;peer, &amp;addrlen);
      if (sockd2 == -1) {
          perror("Wrong connection");
          continue;
      }
      write(sockd2, hello, sizeof(hello));
      // close(sockd2);
  }
  </code></pre>
  
  <p>I'm using Google Chrome to connect to localhost:8080 and as is above, the page loads just fine.</p>
  
  <p>If I uncomment the <code>close</code> call and <code>Connection: close</code> line, the page won't load and instead the developer console reads <code>Failed to load resource: net::ERR_CONNECTION_RESET</code>.</p>
  
  <p>If I do the above but also uncomment one of the other http header lines, it loads.</p>
  
  <p>What's going on?</p>