title: Download HTTP thru sockets (C)
body: <p>Recently I started taking <a href="http://beej.us/guide/bgnet/" rel="noreferrer">this guide</a> to get myself started on downloading files from the internet. I read it and came up with the following code to download the HTTP body of a website. The only problem is, it's not working. The code stops when calling the recv() call. It does not crash, it just keeps on running. Is this my fault? Am I using the wrong approch? I intent to use the code to not just download the contents of .html-files, but also to download other files (zip, png, jpg, dmg ...). I hope there's somebody that can help me. This is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt; /* SOCKET */
  #include &lt;netdb.h&gt; /* struct addrinfo */
  #include &lt;stdlib.h&gt; /* exit() */
  #include &lt;string.h&gt; /* memset() */
  #include &lt;errno.h&gt; /* errno */
  #include &lt;unistd.h&gt; /* close() */
  #include &lt;arpa/inet.h&gt; /* IP Conversion */
  
  #include &lt;stdarg.h&gt; /* va_list */
  
  #define SERVERNAME "developerief2.site11.com"
  #define PROTOCOL "80"
  #define MAXDATASIZE 1024*1024
  
  void errorOut(int status, const char *format, ...);
  void *get_in_addr(struct sockaddr *sa);
  
  int main (int argc, const char * argv[]) {
      int status;
  
      // GET ADDRESS INFO
      struct addrinfo *infos; 
      struct addrinfo hints;
  
      // fill hints
      memset(&amp;hints, 0, sizeof(hints));
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_flags = AI_PASSIVE;
      hints.ai_family = AF_UNSPEC;
  
      // get address info
      status = getaddrinfo(SERVERNAME, 
                           PROTOCOL, 
                           &amp;hints, 
                           &amp;infos);
      if(status != 0)
          errorOut(-1, "Couldn't get addres information: %s\n", gai_strerror(status));
  
      // MAKE SOCKET
      int sockfd;
  
      // loop, use first valid
      struct addrinfo *p;
      for(p = infos; p != NULL; p = p-&gt;ai_next) {
          // CREATE SOCKET
          sockfd = socket(p-&gt;ai_family, 
                          p-&gt;ai_socktype, 
                          p-&gt;ai_protocol);
          if(sockfd == -1)
              continue;
  
          // TRY TO CONNECT
          status = connect(sockfd, 
                           p-&gt;ai_addr, 
                           p-&gt;ai_addrlen);
          if(status == -1) {
              close(sockfd);
              continue;
          }
  
          break;
      }
  
      if(p == NULL) {
          fprintf(stderr, "Failed to connect\n");
          return 1;
      }
  
      // LET USER KNOW
      char printableIP[INET6_ADDRSTRLEN];
      inet_ntop(p-&gt;ai_family,
                get_in_addr((struct sockaddr *)p-&gt;ai_addr),
                printableIP,
                sizeof(printableIP));
      printf("Connection to %s\n", printableIP);
  
      // GET RID OF INFOS
      freeaddrinfo(infos);
  
      // RECEIVE DATA
      ssize_t receivedBytes;
      char buf[MAXDATASIZE];
      printf("Start receiving\n");
      receivedBytes = recv(sockfd, 
                           buf, 
                           MAXDATASIZE-1, 
                           0);
      printf("Received %d bytes\n", (int)receivedBytes);
      if(receivedBytes == -1)
          errorOut(1, "Error while receiving\n");
  
      // null terminate
      buf[receivedBytes] = '\0';
  
      // PRINT
      printf("Received Data:\n\n%s\n", buf);
  
      // CLOSE
      close(sockfd);
  
      return 0;
  }
  
  void *get_in_addr(struct sockaddr *sa) {
      // IP4
      if(sa-&gt;sa_family == AF_INET)
          return &amp;(((struct sockaddr_in *) sa)-&gt;sin_addr);
  
      return &amp;(((struct sockaddr_in6 *) sa)-&gt;sin6_addr);
  }
  
  void errorOut(int status, const char *format, ...) {
      va_list args;
      va_start(args, format);
      vfprintf(stderr, format, args);
      va_end(args);
      exit(status);
  }
  </code></pre>