title: File transfer through sockets, final size with less bytes
body: <p>I'm trying to receive some file through sockets in C. But the server sends me 64-byte packets for a 1000000 byte file for example and I get approximately 999902 bytes on the destination file.</p>
  
  <pre><code>while ((n = read(sd, buffer_in, BUFSIZE ))) //  BUFSIZE = 64 
  {
      if(n&lt;0)
      {
         printf("Fail.\n");
         fclose(archivo);
         return -1;
      }
  
      if(fwrite(buffer_in, n, 1, f) !=1 ) 
      { 
         printf("fwrite error.\n");
         fclose(archivo);
         return -1;
      }
  
      bytes+=n;
  }
  
  printf("We received %d bytes",  bytes);
  </code></pre>
  
  <p>When used through a local TCP/IP socket it works, but not in a slow connection. I see through debugging that I get a lot of 64 byte chunks, and a 30 byte chunk near EOF. I know that you can get less bytes on read() since the call returns when any data (>1 byte) is available. But this condition shouldn't be catched by the while? Should return when n == 0, that is no more data (EOF).</p>
  
  <p>Thx for your help.</p>
  
  <p>(EDIT)</p>
  
  <p>Sending code as follows:</p>
  
  <pre><code>while (n=read(file_fd, buffer, BUFSIZE))
  {
     write (sdaccept, buffer, n)
  }
  </code></pre>
  
  <p>I know that both read() and write () may return N &lt; BUFSIZE, but shouldn't this loop work out that accordingly? I added up n and returns 1000000, the exact size.</p>
  
  <p>(EDIT II)</p>
  
  <p>Tested with a C source with 10673  bytes, receives 10575 without corruption, except that the destination file LACKS the first 98 bytes!!!</p>