title: Creating HTTP client to download a webpage for offline viewing in C
body: <p>I am creating a HTTP client which downloads a web page based on a command line argument. It takes the argument, looks up the domain name to get the IP address, creates a socket, connects to the server and sends a GET request and waits for a reply. This all works fine however when I read my reply in using a buffer and a while loop I also receive some unreadable characters. If you run the code and view the html you will see unreadable characters here and there on the page.</p>
  
  <p>My Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  int main(int argc, char *argv[])
  {
      int socket_desc, i, bytes_read;    
      char server_reply[1024], ip[100], request[100];;
      char *hostname = argv[1];
      struct sockaddr_in server;
      struct hostent *he;
      struct in_addr **addr_list;
      FILE *fp;
  
      if ((he = gethostbyname(hostname)) == NULL) {
          //gethostbyname failed
          herror("gethostbyname\n");
          return 1;
      }
  
      addr_list = (struct in_addr **) he-&gt;h_addr_list;
  
      for(i = 0; addr_list[i] != NULL; i++) {
          //Return the first one;
          strcpy(ip , inet_ntoa(*addr_list[i]) );
      }
  
      //Create socket
      socket_desc = socket(AF_INET, SOCK_STREAM, 0);
      if (socket_desc == -1) {
          printf("Could not create socket!\n");
      }
  
      server.sin_addr.s_addr = inet_addr(ip);
      server.sin_family = AF_INET;
      server.sin_port = htons(80);
  
      //Connect to remote server
      if (connect(socket_desc , (struct sockaddr *)&amp;server , sizeof(server)) &lt; 0) {
          printf("connect error!\n");
          return 1;
      }
  
      printf("Connected...\n");
  
      //Send some data
      snprintf(request, 99, "GET / HTTP/1.1\r\n"
              "Host: %s\r\n"
              "\r\n\r\n", hostname
      );
  
      if (send(socket_desc, request, strlen(request), 0) &lt; 0) {
          puts("Send failed!\n");
          return 1;
      }
      puts("Data Sent...\n");
  
      //Receive a reply from the server
  
      fp = fopen("/home/localusr/Desktop/ouput.html", "w+");
  
      while (bytes_read = read(socket_desc, server_reply, sizeof(server_reply)) &gt; 0) {
          fputs(server_reply, fp);
          memset(server_reply, 0, sizeof(server_reply));
      } 
      do {
          bytes_read = read(socket_desc, server_reply, sizeof(server_reply));
          fputs(server_reply, fp);
          memset(server_reply, 0, sizeof(server_reply));
      } while (bytes_read &gt; 0);
  
      printf("reply received...\n");
  
      fclose(fp);
      close(socket_desc);
  
      return 0;
  }
  </code></pre>
  
  <p>Sorry about the poorly indented code. Any help much appreciated. I am using an Ubuntu machine and using gcc to compile my code.</p>
  
  <p>EDIT:</p>
  
  <pre><code>orb.ws.require.lib--&gt; &lt;script type="text/javascript"&gt;/*
  be2
  </code></pre>
  
  <p>the be2 should not be there. * also get '@' symbols   
    </p>