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.
while ((n = read(sd, buffer_in, BUFSIZE ))) // BUFSIZE = 64
{
if(n<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);
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).
Thx for your help.
(EDIT)
Sending code as follows:
while (n=read(file_fd, buffer, BUFSIZE))
{
write (sdaccept, buffer, n)
}
I know that both read() and write () may return N < BUFSIZE, but shouldn't this loop work out that accordingly? I added up n and returns 1000000, the exact size.
(EDIT II)
Tested with a C source with 10673 bytes, receives 10575 without corruption, except that the destination file LACKS the first 98 bytes!!!