问题描述
如何确定是否可以将给定的字节数写入文件句柄(实际上是套接字)? (或者,如何未读"我从其他文件句柄读取的数据?)
我想要类似的东西:
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中,而不使用写债务"缓冲区.
通常,如何简单可靠地将数据从一个文件句柄复制到另一个文件句柄?
@related 如何互连"? Linux中有两个套接字?
我认为没有任何接口可以让您访问该信息,无论如何,它都会过时.
我建议将两个文件描述符都设置为非阻塞,然后读取/写入1K(可能更大)的块,直到获得EAGAIN/EWOULDBLOCK,此时应该缓存一个块,直到下一次写入fd准备就绪为止. /p>
无论如何,您都需要有一个缓冲区来执行读/写周期,因此为写债务保留缓冲区应该是一个太大的问题?
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?
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?
这篇关于确定我可以向文件句柄中写入多少内容;将数据从一个FH复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!