Windows 上的反向 dns 查找会阻塞几秒钟以获取无法解析的 IP 地址

本文介绍了Windows 上的反向 dns 查找会阻塞几秒钟以获取无法解析的 IP 地址的处理方法,对大家解决问题具有一定的参考价值

问题描述

我正在使用 Java 的 InetAddress.getHostName() 执行一些反向 DNS 查找,但它所花费的时间似乎有问题.这是一个代码片段:

I'm using Java's InetAddress.getHostName() to perform some reverse DNS lookups, and something seems wrong with the time it takes. Here's a code snippet:

public static void main(String[] args) throws IOException {

byte[][] addresses = new byte[][] { { 10, (byte) 0, (byte) 0, (byte) 138 }
    , new byte[] { (byte) 216, (byte) 239, (byte) 49, (byte) 245 }
    ,{ 8, (byte) 8, (byte) 8, (byte) 8 } };

    for (byte[] addr : addresses) {
        InetAddress inet = InetAddress.getByAddress(addr);
        long before = System.currentTimeMillis();
        String hostName = inet.getHostName();
        System.out.printf("%20s %40s %5d
", inet.getHostAddress(), hostName, (System.currentTimeMillis() - before));
    }
}

这是我机器上的输出:

    10.0.0.138                               10.0.0.138  4503
216.239.49.245                           216.239.49.245  4591
       8.8.8.8           google-public-dns-a.google.com     8

无论我运行此代码多少次,解析 10.0.0.138 和 216.239.49.245 都需要 4.5 秒.这似乎发生在所有无法解析的 IP 地址上.

Resolving both 10.0.0.138 and 216.239.49.245 takes 4.5 seconds each, regardless of the number of times I've ran this code. This seems to happen with all unresolvable IP addresses.

这不是网络问题,因为根据 wireshark 的捕获,运行此代码时,DNS 查询甚至不会发送除非首先清除 DNS 缓存(然后结果甚至更慢 - 每个分辨率大约 4.7 秒).

This isn't a networking issue since according to a wireshark capture, DNS queries aren't even sent when this code is run unless the DNS cache is cleared first (and then the results are even slower - around 4.7 seconds per resolution).

那么,Java 是否真的需要 4.5 秒才能针对操作系统的本地 DNS 缓存超时?这是没有意义的.命令行实用程序 nslookup 更快地返回(无法解析)这些 IP 地址的结果,甚至不使用缓存!

So does it actually take Java 4.5 seconds to timeout against the OS's local DNS cache? That makes no sense. The command line utility nslookup returns (unresolvable) results for these IP addresses much faster, and it doesn't even use the cache!

有人可以解释这种行为并提出加快这些解决方案的方法吗?在不求助于外部库的情况下,我唯一能想到的就是使用多个线程,因此至少 4.5 秒的超时将并行执行.

Can some one explain this behavior and suggest way to speed up these resolutions? The only thing I can think of without turning to external libraries is using multiple threads so at least the 4.5 second timeout will be executed in parallel.

作为参考,我在 Windows 7 x64 上使用 JDK 7u71

For reference, I'm using JDK 7u71 on Windows 7 x64

编辑1:这个问题似乎相关,但答案那里说性能取决于网络,这不是我观察到的.

Edit1: This question seems relevant, but the answer there says that the performance depends on the network, which is not what I observed.

这似乎是一个 Windows 问题.同一 LAN 中的机器使用完全相同的 DNS,运行带有 JDK 1.7u67 的 OpenSuse 13.1 返回以下结果:

This seems to be a windows issue. A machine in the same LAN, that uses the exact same DNS, running OpenSuse 13.1 with JDK 1.7u67 returned the following results:

没有 DNS 缓存:

10.0.0.138                                   10.0.0.138  116             
216.239.49.245                           216.239.49.245  5098             
8.8.8.8                  google-public-dns-a.google.com  301

使用 DNS 缓存:

10.0.0.138                                   10.0.0.138  5
216.239.49.245                           216.239.49.245  9             
8.8.8.8                  google-public-dns-a.google.com  40

最终我不得不使用 dnsjava 进行自己的反向 DNS 查找来解决这个问题.

Eventually I had to work around the issue by doing my own reverse DNS lookups using dnsjava.

推荐答案

看来是 Windows 中 DNS 客户端的实现有问题.我刚刚在 C#.NET 中尝试了相同的逻辑:

It looks like an issue with the implementation of the DNS client in Windows. I just tried the same logic in C#.NET:

 static void Main(string[] args)
    {
        byte[][] addresses = new byte[][] { new byte[] { 10, (byte) 0, (byte) 0, (byte) 138 },
            new byte[] { (byte) 216, (byte) 239, (byte) 49, (byte) 245 },
            new byte []{ 8, (byte) 8, (byte) 8, (byte) 8 } };
        foreach (byte[] addr in addresses)
        {
            IPAddress inet = new IPAddress(addr);
            DateTime before = DateTime.Now;
            String hostName = null;
            try
            {
                hostName = System.Net.Dns.GetHostByAddress(inet).HostName;
            }
            catch { }
            finally
            {
                DateTime after = DateTime.Now;
                Console.WriteLine("{0} {1} {2}", inet.ToString(), hostName!=null?hostName:"N/A", after.Subtract(before));
            }
        }
        Console.ReadLine();
    }

这些是结果:

   10.0.0.138 N/A 00:00:04.5604560
   216.239.49.245 N/A 00:00:04.7984798
   8.8.8.8 google-public-dns-a.google.com 00:00:00.0060006

有趣的是,在 DNS 缓存刷新后,Windows 会将所有 DNS 请求发送到网络.DNS 服务器会在 0.25 秒后回复,但如果答案是没有这样的名称",DNS 客户端仍然会在整个超时期间阻塞.

The interesting part is that after a DNS cache flush, Windows sends all DNS requests to the network. The DNS server replies after 0.25 second but if the answer is "No Such Name", the DNS client still blocks for the entire duration of the timeout.

这篇关于Windows 上的反向 dns 查找会阻塞几秒钟以获取无法解析的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1172

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1071

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:808

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:900

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:877

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:865

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:834

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1733

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1309