I'm trying to get some file via sockets in C. But the server sends me 64 byte packets for a 1,000,000 byte file, and I get about 999902 bytes in the target 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 over 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 the EOF. I know that you can get fewer bytes on read (), as the call returns when any data is available (> 1 byte). But this condition should not be caught by time? Should return when n == 0, this is no longer data (EOF).
Thanks for your help.
(EDIT)
Sending the code as follows:
while (n=read(file_fd, buffer, BUFSIZE)) { write (sdaccept, buffer, n) }
I know that both read () and write () can return N <BUFSIZE, but should this loop not work properly? I added n and returns 1,000,000, the exact size.
(EDIT II)
Tested with source C with 10673 bytes, gets 10575 without damage, except that the LACKS destination file contains the first 98 bytes !!!
c sockets file-transfer
HernΓ‘n
source share