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