Phong
Phong

Reputation: 6768

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

Upvotes: 3

Views: 4278

Answers (1)

Andreas Bonini
Andreas Bonini

Reputation: 44752

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

Upvotes: 6

Related Questions