title: C++ - Simple server which sends simple HTML to clients
body: <p>Now, I'm just fooling around with this and I'm not sure as to why this isn't working.</p>
  <pre><code>#include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cassert&gt;
  
  const char html[] = &quot;HTTP/1.1 200 OK\r\n&quot;
  &quot;Connection: close\r\n&quot;
  &quot;Content-type: text/html\r\n&quot;
  &quot;\r\n&quot;
  &quot;&lt;html&gt;\r\n&quot;
  &quot;&lt;head&gt;\r\n&quot;
  &quot;&lt;title&gt;Hello, world!&lt;/title&gt;\r\n&quot;
  &quot;&lt;/head&gt;\r\n&quot;
  &quot;&lt;body&gt;\r\n&quot;
  &quot;&lt;h1&gt;Hello, world!&lt;/h1&gt;\r\n&quot;
  &quot;&lt;/body&gt;\r\n&quot;
  &quot;&lt;/html&gt;\r\n\r\n&quot;;
  
  int main() {
      WSADATA wsa;
  
      assert( WSAStartup( MAKEWORD( 2, 2 ), &amp;wsa ) == 0 );
  
      addrinfo *res = NULL;
      addrinfo hints;
  
      ZeroMemory( &amp;hints, sizeof( hints ) );
  
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
      assert( getaddrinfo( NULL, &quot;80&quot;, &amp;hints, &amp;res ) == 0 );
  
      SOCKET s = socket( res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol );
  
      assert( s != INVALID_SOCKET );
      assert( bind( s, res-&gt;ai_addr, (int)res-&gt;ai_addrlen ) != SOCKET_ERROR );
      assert( listen( s, SOMAXCONN ) != SOCKET_ERROR );
  
      SOCKET client = accept( s, NULL, NULL );
  
      assert( client != INVALID_SOCKET );
  
      char buffer[512];
      int bytes;
  
      bytes = recv( client, buffer, 512, 0 );
  
      for ( int i = 0; i &lt; bytes; ++i ) {
          std::cout &lt;&lt; buffer[i];
      }
  
      assert( send( client, html, strlen( html ) - 1, 0 ) &gt; 0 );
      assert( shutdown( client, SD_BOTH ) != SOCKET_ERROR );
  
      closesocket( client );
      WSACleanup();
  
      return 0;
  }
  </code></pre>
  <p>When I compile and run this and then navigate to 127.0.0.1 in my browser I get this in my console:</p>
  <blockquote>
  <p>GET / HTTP/1.1</p>
  <p>Host: 127.0.0.1</p>
  <p>Connection: keep-alive</p>
  <p>User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.5 (K HTML, like Gecko) Chrome/2.0.172.8 Safari/530.5</p>
  <p>Cache-Control: max-age=0</p>
  <p>Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,<em>/</em>;q=0.5</p>
  <p>Accept-Encoding: gzip,deflate,bzip2,sdch</p>
  <p>Accept-Language: en-US,en;q=0.8</p>
  <p>Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3</p>
  </blockquote>
  <p><strong>EDIT</strong> - I've updated the HTML I'm sending. I just tested this with Mozilla Firefox and Google Chrome and it works in Firefox, but not Chrome!</p>
  <p><strong>EDIT 2</strong> - So it seems that the reason it was working on Firefox, but not Chrome, was because Firefox displays the HTML as its received, while Chrome waits for the connection to close before doing any rendering. I added the code to close the socket and it worked. I've updated my code with the working source.</p>