GetHostEntry很慢 [英] GetHostEntry is very slow

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

问题描述

我有一个WinForms应用程序,我正在尝试获得反向DNS条目列表中显示的IP列表。

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.

唯一的其他选项是使用某种形式的后台处理,例如这个异步版本

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 ,有一个flags参数。我相信你可以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天全站免登陆