问题描述
我如何知道如果读取套接字缓冲区已满或写套接字缓冲区是空的?
How can I tell if a read socket buffer is full or a write socket buffer is empty?
有没有一种方法可以让我得到一个套接字缓冲区的状态,没有一个系统调用?
Is there a way I can get the status of a socket buffer without a system call?
更新:这个怎么样:我想获得一个回调或信号时,无论是读套接字缓冲区已满或写套接字缓冲区是空的。在电线上发送数据时,这样我可以停止处理,使线路上出现更多的I / O,因为是I / O密集型始终是一个问题。
UPDATE: How about this: I'd like to get a callback or signal when either the read socket buffer is full or the write socket buffer is empty. This way I can stop processing to allow more I/O to occur on the wire, since being I/O bound is always an issue when sending data on the wire.
的选择()
通话是你如何检查读取缓冲区中有一些东西。的不的时候才满(我认为)。
The select()
call is how you check if the read buffer has something in it. Not when it is full (I think).
推荐答案
您可以尝试的ioctl
。 FIONREAD告诉你有多少个字节是立即读取。如果这是在相同的缓冲大小(您可能能够检索和/或与另一icotl呼叫设置),则缓冲器已满。同样地,如果可以写入的字节数作为输出缓冲器的大小,则输出缓冲器是空的。
You can try ioctl
. FIONREAD tells you how many bytes are immediately readable. If this is the same as the buffer size (which you might be able to retrieve and/or set with another icotl call), then the buffer is full. Likewise, if you can write as many bytes as the size of the output buffer, then the output buffer is empty.
我不知道如何得到广泛支持FIONREAD,FIONWRITE和SIOCGIFBUFS(或当量)。我不知道我曾经使用过其中任何一个,但我偷偷摸摸的感觉,我已经使用在Symbian类似的功能出于某种原因或其他。
I don't how widely supported FIONREAD, FIONWRITE, and SIOCGIFBUFS (or equivalents) are. I'm not sure I've ever used any of them, although I've a sneaky feeling I've used similar functionality on Symbian for some reason or other.
是否需要调用内核模式来计算,这是特定于平台。依稀试图避免系统调用是不是一个有效的优化技术。
Whether the call needs kernel mode to compute this is platform-specific. Vaguely trying to avoid system calls is not a valid optimisation technique.
一个基本的BSD风格的套接字接口不出声太多关于读取和写入缓冲区。当它的问题发送缓冲区是否是空的?这当然并不意味着所有的数据已经在插座的另一个端点已经接收 - 它可以被某处坐在一些路由器。同样的,你读缓冲区已满并不能保证在另一端的写将会阻塞。
A basic BSD-style sockets interface doesn't say anything much about read and write buffers. When does it matter whether the send buffer is empty? It certainly doesn't mean that all the data has been received at the other endpoint of the socket - it could be sitting in some router somewhere. Likewise, "your" read buffer being full doesn't guarantee that a write at the other end will block.
一般来说,你刚读/写尽可能多的,你可以让套接字层处理的复杂性。如果你看到了很多的I / O与小尺寸完成那么也许有一些性能问题。但请记住,一个流套接字将发送/在同一时间收到一个数据包,包含数据块。除非设置了TCP_NODELAY,这并不是说字节由那些在NIC到达,你可能最终使每个字节一个读调用。他们在到达的数据包,因此最有可能成为可读全部一次,也许1K上下的一次。你不太可能能够通过持观望读数,直到有很多读来加快速度。事实上,你可能使情况变得更糟,因为您的端点的读取缓冲区已满的时候,有一个风险,即输入数据被丢弃,因为没有地方存放它,造成延误和重新发送。
Generally speaking, you just read/write as much as you can and let the sockets layer handle the complexity. If you're seeing a lot of I/O completed with tiny sizes then maybe there's some performance problem. But remember that a stream socket will send/receive a packet at a time, containing a block of data. Unless TCP_NODELAY is set, it's not as though bytes are arriving by ones at the NIC, and you might end up making one read call per byte. They're arriving in packets, so most likely will become readable all at once, perhaps 1k-ish at a time. You're unlikely to be able to speed things up by holding off reading until there's a lot to read. In fact you might make it worse, because by the time your endpoint's read buffer is full, there's a risk that incoming data is being discarded because there's nowhere to store it, resulting in delays and re-sends.
这篇关于我怎样才能知道,如果一个套接字缓冲区满了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!