确定我可以向文件句柄写入多少内容;将数据从一个 FH 复制到另一个 FH

发布于 2024-08-29 16:34:06 字数 491 浏览 13 评论 0原文

如何确定是否可以将给定数量的字节写入文件句柄(实际上是套接字)? (或者,如何“取消读取”我从其他文件句柄读取的数据?)

我想要类似的内容:

n = how_much_can_I_write(w_handle);
n = read(r_handle, buf, n);
assert(n==write(w_handle, buf, n));

两个文件句柄(r_handle 和 w_handle)都已从 epoll_wait 接收到就绪状态。

我希望将 r_handle 中的所有数据复制到 w_handle 而不使用“写债务”缓冲区。

一般来说,如何简单可靠地将数据从一个文件句柄复制到另一个文件句柄?

@相关 如何在 Linux 中“互连”两个套接字?

How to determine if I can write the given number of bytes to a filehandle (socket actually)? (Alternatively, how to "unread" the data I had read from other filehandle?)

I want something like:

n = how_much_can_I_write(w_handle);
n = read(r_handle, buf, n);
assert(n==write(w_handle, buf, n));

Both filehandles (r_handle and w_handle) have received ready status from epoll_wait.

I want all data from r_handle to be copied to w_handle without using a "write debt" buffer.

In general, how to copy the data from one filehandle to the other simply and reliably?

@related How can I "interconnect" two sockets in Linux?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-09-05 16:34:06

你不能这样做 - 一旦数据被写入,它就被写入 - 该操作是不可逆的或提前可预测的。你需要重新思考你的程序逻辑。

You can't do that - once the data is written, it's written - the operation is not reversible or predictable in advance. You need to rethink your program logic.

甩你一脸翔 2024-09-05 16:34:06

我认为没有任何界面可以让您访问该信息,而且一旦您获得它,它就会过时。

我建议将两个文件描述符设置为非阻塞,然后读/写 1K(可能更大)块,直到获得 EAGAIN/EWOULDBLOCK,这时您应该缓存一个块,直到下一次写入 fd 准备好为止。

无论如何,您都需要有一个缓冲区来执行读/写周期,因此为写债务保留缓冲区应该是一个太大的问题?

I don't think there's any interface that allows you access to that information, and it would be stale as soon as you got it anyway.

I'd suggest setting both file descriptors to non-blocking, then reading/writing 1K (maybe larger) blocks until you get EAGAIN/EWOULDBLOCK, when you should cache one block until the next time the write fd is ready.

You need to have a buffer for doing the read/write cycle anyway, so keeping the buffer for the write-debt should be too much of a problem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文