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
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. In this case 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 in a single 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.
You can also take a look at the man page for sendfile()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With