GetHostEntry 很慢 [英] GetHostEntry is very slow

查看:26
本文介绍了GetHostEntry 很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WinForms 应用程序,我正在尝试获取表单上显示的 IP 列表的反向 DNS 条目.

I have a WinForms app, and I'm trying to get reverse DNS entries for a list of IPs displayed on the form.

我遇到的主要问题是 System.Net.Dns.GetHostEntry 慢得离谱,尤其是当没有找到反向 DNS 条目时.使用直接 DNS,这应该很快,因为 DNS 服务器将返回 NXDOMAIN.在内部,它调用 ws2_32.dll getnameinfo(),其中指出名称解析可以通过域名系统 (DNS)、本地主机文件或其他命名机制" - 所以我假设是那些其他命名机制"导致它变得如此缓慢,但是有谁知道这些机制是什么?

The main issue I've run into is System.Net.Dns.GetHostEntry is ridiculously slow, particularly when no reverse DNS entry is found. With straight DNS, this should be fast, since the DNS server will return NXDOMAIN. Internally, it's calling ws2_32.dll getnameinfo(), which states "Name resolution can be by the Domain Name System (DNS), a local hosts file, or by other naming mechanisms" - so I'm assuming it's those "other naming mechanisms" that's causing it to be so slow, but does anyone know what those mechanisms are?

通常每个 IP 需要 5 秒,除非它找到反向条目,然后它几乎是立即的.我已经使用线程部分解决了这个问题,但由于我正在做一个大列表,而且我一次只能运行这么多线程,所以仍然需要一段时间才能完成所有任务.

Generally this is taking 5 seconds per IP, unless it finds a reverse entry, and then it's almost immediate. I've partly worked around this using threads, but since I am doing a large list and I can only run so many threads at once, it still takes a while to get through them all.

有没有更好的方法可以更快地找到反向 DNS 条目?

Is there a better way to find reverse DNS entries that is going to be faster?

推荐答案

不幸的是,没有办法(我知道)在客户端的 Windows API 中更改此超时.您能做的最好的事情是编辑注册表以更改 DNS 查询中的超时长度.有关详细信息,请参阅这篇技术网文章.据我所知,尝试 1, 2, &执行此操作时会运行 3,因此延迟了 5 秒.

Unfortunately, there is no way (of which I am aware) to change this timeout in the Windows API, on the client side. The best you can do is edit the registry to alter the length of the timeouts in DNS queries. See this technet article for details. To my knowledge, attempts 1, 2, & 3 are run when you do this, hence the 5 second delay.

唯一的其他选择是使用某种形式的后台处理,例如异步版本 反向 DNS 查找.但是,这将使用线程,因此您最终会遇到超时(这会更好,因为它将跨越许多等待线程,但仍然不完美).就个人而言,如果您要处理大量数据,我会混合使用这两种方法 - 异步进行反向查找并修改注册表以缩短超时时间.

The only other option is to use some form of background processing, such as this asynchronous version of reverse DNS lookups. This is going to use threading, though, so you'll eventually run into the timeouts (it'll be better, since it'll be across many waiting threads, but still not perfect). Personally, if you're going to process a huge number, I'd mix both approaches - do a reverse lookup ansyncrhonously AND modify the registry to make the timeout shorter.

评论后

如果您查看 getnameinfo,有一个标志参数.我相信你可以 P/Invoke 进入这个并设置标志 NI_NAMEREQD |NI_NUMERICHOST 以获得您所追求的行为.(第一个说如果没有 DNS 条目立即出错,这有助于超时 - 第二个说进行反向查找.)

If you look at the flags on getnameinfo, there is a flags parameter. I believe you can P/Invoke into this and set the flags NI_NAMEREQD | NI_NUMERICHOST to get the behavior you are after. (The first says to error out immediately if there is no DNS entry, which helps the timeout - the second says to do the reverse lookup.)

这篇关于GetHostEntry 很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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