如何判断套接字缓冲区是否已满? [英] How can I tell if a socket buffer is full?

查看:77
本文介绍了如何判断套接字缓冲区是否已满?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何判断读套接字缓冲区已满或写套接字缓冲区为空?

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.

select() 调用用于检查读取缓冲区中是否有内容.不是满时(我认为).

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-ish.你不太可能通过推迟阅读来加快速度,直到有很多东西要阅读.事实上,您可能会让情况变得更糟,因为当您端点的读取缓冲区已满时,传入的数据可能会因为无处存储而被丢弃,从而导致延迟和重新发送.

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.

这篇关于如何判断套接字缓冲区是否已满?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆