Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sendfile() completion to non-blocking socket

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

like image 383
Phong Avatar asked Jul 28 '26 03:07

Phong


1 Answers

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()

like image 52
Thomas Bonini Avatar answered Jul 30 '26 03:07

Thomas Bonini