views:

167

answers:

1

Hi,

In my program, I need to check the completion of a sendfile() operation in a non-blocking socket. How can that be done?

After checking the documentation and searching on internet, I couldn't find out how to do it

+2  A: 

It works very similarly to send(): if the socket is set as O_NONBLOCK and the operation would block, sendfile() returns immediately and sets errno to EAGAIN.

If it does you have to wait a while and then try again (maybe using a function like select() to know when it's ready).

Also keep in mind that even if it succeeds it may not write all the bytes you requested it to write in one function call. Always check the 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.

Also read the man page: link

Andreas Bonini
To expand slightly: if `sendfile()` returns a value > 0, then that many bytes have now been successfully sent - there's no need to wait for completion. "Non-blocking" does not imply "asychronous".
caf