在选择中响应 ICMP
我感兴趣的基本代码序列是(伪代码)
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)。
我的问题是:
- SIO_UDP_CONNRESET 在这种情况下是最好的方法吗?
- 是否有其他一些方法可以忽略“select”的 ICMP 或过滤它以进行接收(也许,忽略 Windows 上的 WSAECONNRESET 错误将其视为超时,在其他情况下可以触发此错误)吗?
- Linux 和 Unix(Solaris、OpenBSD)上是否存在类似问题?
The basic code sequence I'm interesting for is (pseudocode)
sendto(some host); // host may be unreachable for now which is normal
...
if(select(readfs, timeout)) // there are some data to read
recvfrom();
Since Win2000, ICMP packet, which is sent back after sending UDP datagram to unreachable port, triggers select, after that recvfrom fails with WSAECONNRESET. Such behaviour isn't desirable for me, because I want select to finish with timeout in this case (there are no data to read). On Windows this can be solved with WSAIoctl SIO_UDP_CONNRESET ( http://support.microsoft.com/kb/263823 ).
My questions are:
- Is SIO_UDP_CONNRESET the best way in this situation?
- Are there some other methods to ignore ICMP for "select" or to filter it for recvfrom (maybe, ignoring WSAECONNRESET error on Windows treating it like timeout, can this error be triggered in some other case)?
- Are there similar issues on Linux and Unix (Solaris, OpenBSD)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
select()
的readfds
设置实际上只是报告套接字上的read()
不会阻塞 ——它不承诺是否有实际数据可供读取。我不知道你具体想用两秒超时来完成什么,而不仅仅是永远休眠——也不知道为什么你不能只添加一个
if
块来检查来自
——但如果不能很好地处理这种情况,就会感觉你的设计过于复杂。recvfrom()
的 WSAECONNRESET许多 Linux 系统上的
select_tut(2)
联机帮助页有一些正确使用select()
的指南。以下是最适合您情况的几条规则:select()
'sreadfds
set really just reports that aread()
on the socket won't block -- it doesn't promise anything about whether or not there is actual data available to read.I don't know what specifically you're trying to accomplish with the two-second timeout rather than just sleeping forever -- nor why you can't just add an
if
block to check forWSAECONNRESET
fromrecvfrom()
-- but it feels like you've got an overly-complicated design if it doesn't handle this case well.The
select_tut(2)
manpage on many Linux systems has some guidelines for properly usingselect()
. Here's several rules that seem most apropos to your situation: