Sorry ,maybe my englis is very poor.
I create a socket using SOCK_STREAM,and I
want to transfer a big file,and I know I
must divide the big file,but I don't know
how many datas to divide it?and what is
the reason?
Printable View
Sorry ,maybe my englis is very poor.
I create a socket using SOCK_STREAM,and I
want to transfer a big file,and I know I
must divide the big file,but I don't know
how many datas to divide it?and what is
the reason?
Well send() doesn't guarantee to send a whole block anyway (for a stream), so you need to deal with fragmentation on sending.
Likewise for reading from a file, you may as well read in fixed sized blocks, rather than to try to allocate some xMB buffer you're only going to use once.
One more thing, it's a good idea to send the size of the file first, so the receiver knows how much data to expect.Code:char buff[BUFSIZ];
while ( (n=fread(buff,1,BUFSIZ,fp)) != 0 ) {
char *p = buff;
size_t remainder = n;
while ( remainder != 0 ) {
n = send(sock,p,remainder,0);
if ( n > 0 ) {
// advance by the amount actually sent
p += n;
remainder -= n;
} else if ( n == 0 ) {
// remote disconnected
} else {
// error
}
}
}