gpt4 book ai didi

c - 在选择中响应 ICMP

转载 作者:行者123 更新时间:2023-12-02 20:00:39 25 4
gpt4 key购买 nike

我感兴趣的基本代码序列是(伪代码)

sendto(some host); // host may be unreachable for now which is normal
...
if(select(readfs, timeout)) // there are some data to read
recvfrom();

从Win2000开始,向不可达端口发送UDP数据报后返回的ICMP数据包会触发select,之后recvfrom失败并出现WSAECONNRESET。这种行为对我来说是不可取的,因为我希望 select 在这种情况下以超时完成(没有数据可读取)。在 Windows 上,可以使用 WSAIoctl SIO_UDP_CONNRESET ( http://support.microsoft.com/kb/263823 ) 解决此问题。

我的问题是:

  1. 在这种情况下,SIO_UDP_CONNRESET 是最好的方法吗?
  2. 是否有其他一些方法可以忽略“select”的 ICMP 或过滤它以进行接收(也许,忽略 Windows 上的 WSAECONNRESET 错误将其视为超时,在其他情况下是否会触发此错误)?
  3. Linux 和 Unix(Solaris、OpenBSD)上是否存在类似问题?

最佳答案

select()readfds 设置实际上只是报告套接字上的 read() 不会阻塞 ——它不 promise 是否有实际数据可供读取。

我不知道你具体想用两秒超时来完成什么,而不仅仅是永远休眠——也不知道为什么你不能只添加一个 if block 来检查来自 recvfrom()WSAECONNRESET ——但如果不能很好地处理这种情况,就会感觉你的设计过于复杂。

select_tut(2)许多 Linux 系统上的联机帮助页都有一些正确使用 select() 的指南。以下是最适合您情况的几条规则:

   1.  You should always try to use select() without a timeout.
Your program should have nothing to do if there is no
data available. Code that depends on timeouts is not
usually portable and is difficult to debug.

...

3. No file descriptor must be added to any set if you do not
intend to check its result after the select() call, and
respond appropriately. See next rule.

4. After select() returns, all file descriptors in all sets
should be checked to see if they are ready.

关于c - 在选择中响应 ICMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8383107/

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