title: HTTP Request using Sockets in C
body: <p>SO. I'm trying to make a C application that retrieves the .html file from a server, for example <a href="http://example.com" rel="noreferrer">www.example.com</a>. For this I'm using Sockets and <code>connect</code> <code>send</code> and <code>recv</code> methods. My implementation looks like this:</p>
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  int main(void) {
      //Stream sockets and rcv()
      
      struct addrinfo hints, *res;
      int sockfd;
      
      char buf[2056];
      int byte_count;
      
      //get host info, make socket and connect it
      memset(&amp;hints, 0,sizeof hints);
      hints.ai_family=AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      getaddrinfo(&quot;www.example.com&quot;,&quot;80&quot;, &amp;hints, &amp;res);
      sockfd = socket(res-&gt;ai_family,res-&gt;ai_socktype,res-&gt;ai_protocol);
      printf(&quot;Connecting...\n&quot;);
      connect(sockfd,res-&gt;ai_addr,res-&gt;ai_addrlen);
      printf(&quot;Connected!\n&quot;);
      char *header = &quot;GET /index.html HTTP/1.1\nHost: www.example.com\n&quot;;
      send(sockfd,header,sizeof header,0);
      printf(&quot;GET Sent...\n&quot;);
      //all right ! now that we're connected, we can receive some data!
      byte_count = recv(sockfd,buf,sizeof buf,0);
      printf(&quot;recv()'d %d bytes of data in buf\n&quot;,byte_count);
      printf(&quot;%s&quot;,buf);
      return 0;
  }
  </code></pre>
  <p>But the thing is that it gets stuck at the <code>recv</code> for some seconds, then the buffer <code>buf</code> is filled with this:</p>
  <pre><code>HTTP/1.0 408 Request Timeout
  Content-Type: text/html
  Content-Length: 431
  Connection: close
  Date: Tue, 26 May 2015 23:08:46 GMT
  Server: ECSF (fll/0781)
  
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?&gt;
  &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
           &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
      &lt;head&gt;
          &lt;title&gt;408 - Request Timeout&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;408 - Request Timeout&lt;/h1&gt;
          &lt;div&gt;Server timeout waiting for the HTTP request from the client.&lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Apparently the server never gets my GET string or it might be mal-formed, what is the correct way to go around with this?</p>
  <p>I already downloaded libcurl and even got the http response in a file (which is great so I can process it later) but I was eager to do it all by hand.</p>
  <p>What am I missing here?</p>