gpt4 book ai didi

c - sendfile() 完成到非阻塞套接字

转载 作者:行者123 更新时间:2023-12-01 16:01:45 26 4
gpt4 key购买 nike

在我的程序中,我需要检查非阻塞套接字中 sendfile() 操作的完成情况。怎么办?

查了资料,上网查了下,没找到方法

最佳答案

它的工作方式与 send() 非常相似:如果套接字设置为 O_NONBLOCK 并且操作会阻塞,sendfile() 返回立即并将 errno 设置为 EAGAIN。在这种情况下,您必须稍等片刻,然后重试(可能使用 select() 之类的函数来了解它何时准备就绪)。

另外请记住,即使它成功了,它也可能不会在单个函数调用中写入您请求的所有字节。始终检查返回值:

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.

您还可以查看 man page for sendfile()

关于c - sendfile() 完成到非阻塞套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1936037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com