使用 kqueue 确定套接字另一侧的挂起或套接字的异常状态

发布于 2024-09-12 09:11:40 字数 182 浏览 16 评论 0原文

我已阅读 man 2 kqueue,但尚未发现如何在不使用 EVFILT_READ注册的情况下获得有关套接字挂起或套接字异常情况的通知EVFILT_WRITE。除此之外,尚不完全清楚 kqueue 如何发出套接字的异常状态信号。

感谢您提前的答复。

I've read man 2 kqueue but have not found out how I can get notified about a socket hangup or exceptional condition of the socket without registering it with EVFILT_READ or EVFILT_WRITE. Apart from this it is not fully clear how kqueue signals exceptional states of sockets altogether.

Thanks for your answer in advance.

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

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

发布评论

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

评论(1

乞讨 2024-09-19 09:11:40

在忽略所有 READ 事件的同时获取 EOL 事件的一个技巧是为 NOTE_LOWAT 提供一个高得离谱的值,从而抑制所有 READ 事件。

下面是一个在 Python REPL 中执行此操作的示例:

Python 2.6.5 (r265:79063, Jul 17 2010, 22:57:01) 
[GCC 4.2.1 20070719  [FreeBSD]] on freebsd8
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> import socket
>>> import sys
>>> a, b = socket.socketpair()
>>> kq = select.kqueue()
>>> kq.control([select.kevent(a, select.KQ_FILTER_READ, select.KQ_EV_ADD, select.KQ_NOTE_LOWAT, sys.maxint)], 0)
[]
>>> b.send('abc')
3
>>> kq.control(None, 10) # Interrupt after some time.
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> b.close()
>>> kq.control(None, 10) # Immediate return.
[<select.kevent ident=3 filter=-1 flags=0x8000 fflags=0x0 data=0x3 udata=0x0>]
>>> 

此外,FreeBSD 上不存在异常状态之类的东西,引用 man 2 select

唯一的例外情况
可检测到的是带外数据
在套接字上接收。

A trick that can be used to get EOL events while ignoring all READ events is to supply a ridiculously high value to NOTE_LOWAT, thus suppressing all READ events.

Here's an example doing this in a Python REPL:

Python 2.6.5 (r265:79063, Jul 17 2010, 22:57:01) 
[GCC 4.2.1 20070719  [FreeBSD]] on freebsd8
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> import socket
>>> import sys
>>> a, b = socket.socketpair()
>>> kq = select.kqueue()
>>> kq.control([select.kevent(a, select.KQ_FILTER_READ, select.KQ_EV_ADD, select.KQ_NOTE_LOWAT, sys.maxint)], 0)
[]
>>> b.send('abc')
3
>>> kq.control(None, 10) # Interrupt after some time.
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> b.close()
>>> kq.control(None, 10) # Immediate return.
[<select.kevent ident=3 filter=-1 flags=0x8000 fflags=0x0 data=0x3 udata=0x0>]
>>> 

Moreover, there is no such thing as exceptional state on FreeBSD, to quote man 2 select:

The only exceptional condition
detectable is out-of-band data
received on a socket.

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