gpt4 book ai didi

c - 使用 UDP 处理 POLL

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:27 24 4
gpt4 key购买 nike

我一直在编写(用全能的 C 语言)一个在 UDP 中监听的服务器。我有 2 个套接字在同一端口(IPv4 和 IPv6)上监听,我使用 poll() 来监视两个套接字。

那么,在监听时,正确的处理 poll revents erros 的方法是什么?是否可能收到 POLLHUP?如果可能,必须如何处理? POLLERR 也是如此

此外,是否可以接收 POLLRDBAND 或 POLLPRI?如果是,那么它意味着什么以及必须如何处理?

提前致谢。

PS:所有问题都是UDP相关的

最佳答案

POLLHUP 表示套接字已断开连接。因为您的程序正在监听 UDP 套接字,所以您不应出现此错误情况。

但是,您可能会收到 POLLERR,并且应该准备好处理它。 POSIX.1-2008 standard

POLLERR
An error has occurred on the device or stream. This flag is only valid in
the revents bitmask; it shall be ignored in the events member.

并且该错误可能是由于硬件故障引起的。您应该销毁套接字并尝试重新创建它。

与 TCP 不同,UDP 没有将数据包标记为紧急或带外的机制,因此您不应获得 POLLRDBAND 或 POLLPRI。此外,这两个条件都是可选的过时扩展——因此不应根据 POSIX.1-2008 标准使用它们:

Obsolescent
The functionality described may be removed in a future version of this
volume of POSIX.1-2008. Strictly Conforming POSIX Applications and Strictly
Conforming XSI Applications shall not use obsolescent features.

XSI STREAMS
The functionality described is optional. The functionality described is also
an extension to the ISO C standard.

关于c - 使用 UDP 处理 POLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35468555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com