title: range downloads in http
body: <p>I need to download a html page in chunks. I had build a GET reuest whick can download a certain range of data. But i am unsuccessful in doing this in a repetitive manner.
  Basically I have to reciver first 0-99 bytes then 100-199 and so on...
  Also I would be grateful to know how toh know the exact size of receiving file beforehand using c or c++ code.
  Following is my code.
  i have exempted connectig to sockets etc. as it have been done successfully.</p>
  
  <pre><code>int c=0,s=0;
  while(1)
  {
      get = build_get_query(host, page,s);
      c+=1;
      fprintf(stderr, "Query is:\n&lt;&lt;START&gt;&gt;\n%s&lt;&lt;END&gt;&gt;\n", get);
      //Send the query to the server
      int sent = 0;
      cout&lt;&lt;"sending "&lt;&lt;c&lt;&lt;endl;
      while(sent &lt; strlen(get))
      {
          tmpres = send(sock, get+sent, strlen(get)-sent, 0);
          if(tmpres == -1)
          {
              perror("Can't send query");
              exit(1);
          }
          sent += tmpres;
      }
      //now it is time to receive the page
      memset(buf, 0, sizeof(buf));
      int htmlstart = 0;
      char * htmlcontent;
      cout&lt;&lt; "reciving "&lt;&lt;c&lt;&lt;endl;
      while((tmpres = recv(sock, buf, BUFSIZ, 0)) &gt; 0)
      {
          if(htmlstart == 0)
          {
              /* Under certain conditions this will not work.
              * If the \r\n\r\n part is splitted into two messages
              * it will fail to detect the beginning of HTML content
              */
              htmlcontent = strstr(buf, "\r\n\r\n");
              if(htmlcontent != NULL)
              {
                  htmlstart = 1;
                  htmlcontent += 4;
              }
          }
          else
          {
              htmlcontent = buf;
          }
          if(htmlstart)
          {
              fprintf(stdout, htmlcontent);
          }
  
          memset(buf, 0, tmpres);
      }
      if(tmpres &lt; 0)
      {
          perror("Error receiving data");
      }
      s+=100;
      if(c==5)
          break;
  }
  
  
  char *build_get_query(char *host, char *page,int i)
  {
  char *query;
  char *getpage = page;
  
  int j=i+99;
  char tpl[100] = "GET /%s HTTP/1.1\r\nHost: %s\r\nRange: bytes=%d-%d\r\nUser-    Agent: %s\r\n\r\n";
  
  if(getpage[0] == '/')
  {
      getpage = getpage + 1;
      fprintf(stderr,"Removing leading \"/\", converting %s to %s\n", page,  getpage);
  }
  
  query = (char     *)malloc(strlen(host)+strlen(getpage)+8+strlen(USERAGENT)+strlen(tpl)-5);
  sprintf(query, tpl, getpage, host, i , j, USERAGENT);
  return query;
  }
  </code></pre>