title: Web browser does not interpret the image passed via an unsigned char array
body: <p>What I am trying to do basically is to capture the HTTP GET request of the browser via a socket and send that request to the Internet, then capture the reply and send it back to the browser. When the transfer consist only of text, every thing works perfectly. But when downloading an image. browser gives the error "cant display the image because it contains errors". any help would be greatly appreciated. </p>
  
  <pre><code>#include "cc352.h"
  #include &lt;arpa/inet.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  int
  main(int argc, char **argv)
  {
  int                 listenfd, connfd,weblin,webcon,webwrite,n,x,y,w,w1;
  socklen_t           len,wlen;
  struct sockaddr_in  servaddr, cliaddr , webservad , webcliad;
  unsigned char       buff[3072] , buff2[3072] ,ext[5] ,wbuff[100000];
  time_t              ticks;
  int                 yes = 1;
  const char          *ptr;
  
  
  if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0 ){
      fprintf(stderr, "socket creation failed\n");
      exit (1);
       }
  
  bzero(&amp;servaddr, sizeof(servaddr));
  servaddr.sin_family      = AF_INET;
  
  if (inet_pton(AF_INET,"127.0.0.1", &amp;servaddr.sin_addr) &lt;= 0){
      printf("inet_pton error for %s", argv[1]);
      return 1;
       }
  servaddr.sin_port        = htons(4619); 
  
  if ( (bind(listenfd, (SA *) &amp;servaddr, sizeof(servaddr))) &lt; 0) {
      fprintf(stderr, "bind failed\n");
      exit (1);
      fprintf(stdout, "bindd completed\n");
       }
  
  
  if ( (weblin = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0 ){
      fprintf(stderr, "socket creation failed\n");
      exit (1);
       }
  printf("weblin socket created \n");
  
  bzero(&amp;webservad, sizeof(webservad));
  webservad.sin_family      = AF_INET;
  webservad.sin_port        = htons(80); 
  
  if (inet_pton(AF_INET,"208.80.152.211", &amp;webservad.sin_addr) &lt;= 0){
      printf("inet_pton error for %s", argv[1]);
      return 1;
       }
  
  
  if (connect(weblin, (SA *) &amp;webservad, sizeof(webservad)) &lt; 0) {
      printf("weblin connect error");
      return 1; 
       }
  printf("weblin connected \n");
  
  
  if ( listen(listenfd, LISTENQ) &lt; 0) {
      fprintf(stderr, "listen failed\n");
      exit (1);
      fprintf(stderr, "listning\n");
       }
  
  
      len = sizeof(cliaddr);
      if ( (connfd = accept(listenfd, (SA *) &amp;cliaddr, &amp;len)) &lt; 0 ) {
      fprintf(stderr, "accept failed\n");
      exit (1);
       }
  fprintf(stdout, "Connection accepted\n");
  
  
          int d=0;
  
             read(connfd,&amp;buff, 3071); // Reads GET request from browser save it to array buff
             unsigned char bron[strlen(buff)];
  
             for(d=0 ; d&lt;=sizeof(bron) ; d++){
             bron[d]=buff[d];
             }
             write(weblin,bron, sizeof(bron)); // Send the data to Internet
             printf("%s \n",bron);
  
  
             while(d=read(weblin,&amp;wbuff, 100000)&gt;0){ //Reads the reply from Internet and save it to wbuff
             unsigned char wron[strlen(wbuff)];
  
             for(d=0 ; d&lt;=sizeof(wron) ; d++ ){
              wron[d]=wbuff[d];
              }
             write(connfd,wbuff,strlen(wbuff)); //Writes the reply to the browser
             printf("%s \n",wron);
      }
  
  
   close(connfd);
   close(listenfd);
   close(weblin);
  }
  </code></pre>