title: uploading JPEG using HTTP POST in C
body: <p>I am trying to write a code to send a JPEG image to the server using HTTP POST in C. 
  In my code, I am trying to construct the http post request (header, boundary, content etc...). The content is the JPEG image in this case. The image is corrupted on the server side. I am wondering what could be wrong in my code..</p>
  
  <p>For those of you who may suggest using curl: I know curl may save me lot of work, but I am running this code in one of the linux box, which unfortunately does not support curl..</p>
  
  <hr>
  
  <h2>UPDATE: I edited my code a little bit (code already updated below), and compared the two files on both client and server side, on the server side I found that all the 00 are missing... interesting fact but I still cannot figure out why it does this</h2>
  
  <pre><code>#define MAXLINE 38400
  #define FILESIZE 37632
  #define MAXSUB  38016
  
  char boundary[40] = "---------------------------";
  
  ssize_t process_http(int sockfd, char *host, char *page, char *boundary, char *poststr)
  {
      char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
      ssize_t n;
      snprintf(sendline, MAXLINE,
       "POST /%s HTTP/1.0\r\n"
       "Host: %s\r\n"
       "Content-type: multipart/form-data; boundary=%s\r\n"
       "Content-length: %d\r\n\r\n"
       "%s", page, host, boundary, strlen(poststr), poststr);
      write(sockfd, sendline, strlen(sendline));
      while ((n = read(sockfd, recvline, MAXLINE)) &gt; 0) {
      recvline[n] = '\0';
      printf("%s", recvline);
      }
      return n;
  }
  </code></pre>
  
  <p>In MAIN:</p>
  
  <pre><code>//...
  //socket initialization
  //...
  
  
  if ((fp = fopen(filename, "rb")) == NULL){
     printf("File could not be opened\n");
     exit(1);
  }
  else{
     while((ch = getc(fp)) != EOF){
          sprintf( &amp;fileline[strlen(fileline)], "%c", ch );
      }
      fclose(fp);
  }
  
  snprintf(poststr, MAXSUB,
       "--%s\r\nContent-Disposition: form-data;"
       "name=\"file\"; filename=\"%s\"\r\nContent-Type: text/plain\r\n\r\n"
       "%s\r\n\r\n"
       "--%s\r\n"
       "Content-Disposition: form-data; name=\"boxkey\"\r\n\r\n%s\r\n"
       "--%s--", boundary, filename, fileline, boundary, key, boundary);
  
  //...
  //then make socket connection...
  //...
  
  process_http(sockfd, hname, page, boundary, poststr);
  
  //then close socket and return...
  </code></pre>