send a file with sendfile is easy:
stat(fd,&filestat);
sendfile(sockfd,fd,0,filestat.len)
but how to receive a file using sendfile? since I don't know the length of the file, should I send the file length first?
sendfile(fd, sockfd,0, ??)
There seems to be two ways of doing this:
send the filestat.len first
//send end write(sockfd,filestat.len); sendfile(sockfd,fd,&offset,filestat.len); //receive end read(sockfd,&len); sendfile(fd,sockfd,&offset,len)
use a loop in the receive end:
//receive end while(sendfile(fd,sockfd,&offset,BUF_LEN) > 0) { offset += BUF_LEN; }
Which one is better? Should I handle the buffer length specifically? Is there any problem in the first way when the file is quite large?
(I really like the mac os version of sendfile, it will send till the end of file if count is 0)