Lynton
Lynton

Reputation: 287

How to receive a file using sendfile?

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:

  1. 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)
    
  2. 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)

Upvotes: 9

Views: 6857

Answers (4)

Upayan
Upayan

Reputation: 103

I guess using sendfile to receive a file from a socket fd is not possible, as according to man page:

The in_fd argument must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket).

I tried using it but got an error of Illegal seek

If anyone's interested, I can post snippets of my code.

Upvotes: 2

poundifdef
poundifdef

Reputation: 19370

This is a great question!

The other posters are correct: you could call read() or recv() (what is the difference?) repeatedly until either of those returns 0, which indicates end of file (EOF).

However! You should consider first passing the size of the file, as a good practice. This would allow your client to anticipate exactly how much data is coming through the socket, figure out if (for example) there is enough disk space, etc. It allows you to have some sort of sanity-checking before committing to downloading whatever the server tries to send.

(This has its own perils. What if the server sends the wrong size?)

You might also consider sending the file in chunks. This way, if there is an interruption, you have a greater granularity when figuring out how much you've transferred. (The kernel does this for you anyway. But food for thought.)

Sending a single integer (a file size) over the network isn't too difficult, but there are a few tricks to be aware of if you are very worried about portability.

Good luck!

Upvotes: 6

Duck
Duck

Reputation: 27572

If you are a socket on the receiving end of the sendfile it is no different than any other TCP connection. When you reach eof your read or receive will return 0 bytes read. If there is socket communication that precedes and succeeds the sendfile data then, yes, you need some kind of agreed protocol so each side can make sense of what they are sending and receiving.

Upvotes: 0

Michael
Michael

Reputation: 1503

According to manual to sendfile():

RETURN VALUE
       If  the  transfer was successful, the number of bytes written to out_fd
       is returned.  On error, -1 is returned, and errno is set appropriately.

So just use it in a way you use read() for sockets: repeat reading as much as you need until the whole amount of data you need will be read. Sure, paying attention to the cases of -1 and 0 results.

Upvotes: 1

Related Questions