title: Sending images over sockets
body: <p>I'm trying to send an image file through a TCP socket in C, but the image isn't being reassembled correctly on the server side. I was wondering if anyone can point out the mistake?</p>
  <p>I know that the server is receiving the correct file size and it constructs a file of that size, but it isn't an image file.</p>
  <p>Client</p>
  <pre><code>//Get Picture Size
  printf(&quot;Getting Picture Size\n&quot;);
  FILE *picture;
  picture = fopen(argv[1], &quot;r&quot;);
  int size;
  fseek(picture, 0, SEEK_END);
  size = ftell(picture);
  
  //Send Picture Size
  printf(&quot;Sending Picture Size\n&quot;);
  write(sock, &amp;size, sizeof(size));
  
  //Send Picture as Byte Array
  printf(&quot;Sending Picture as Byte Array\n&quot;);
  char send_buffer[size];
  while(!feof(picture)) {
      fread(send_buffer, 1, sizeof(send_buffer), picture);
      write(sock, send_buffer, sizeof(send_buffer));
      bzero(send_buffer, sizeof(send_buffer));
  }
  </code></pre>
  <p>Server</p>
  <pre><code>//Read Picture Size
  printf(&quot;Reading Picture Size\n&quot;);
  int size;
  read(new_sock, &amp;size, sizeof(int));
  
  //Read Picture Byte Array
  printf(&quot;Reading Picture Byte Array\n&quot;);
  char p_array[size];
  read(new_sock, p_array, size);
  
  //Convert it Back into Picture
  printf(&quot;Converting Byte Array to Picture\n&quot;);
  FILE *image;
  image = fopen(&quot;c1.png&quot;, &quot;w&quot;);
  fwrite(p_array, 1, sizeof(p_array), image);
  fclose(image);
  </code></pre>
  <p>Edit: Fixed sizeof(int) in server code.</p>