Java JNDI DNS错误:“接收超时" [英] Java JNDI DNS error: "Receive timed out"

查看:227
本文介绍了Java JNDI DNS错误:“接收超时"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;

public class DNSLookup
{
    public static void main(String args[])
    {
        String host = "www.google.com";
        try
        {
            InetAddress inetAddress = InetAddress.getByName(host);
            // show the Internet Address as name/address
            System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());

            // get the default initial Directory Context
            InitialDirContext iDirC = new InitialDirContext();
            // get the DNS records for inetAddress

            Attributes attributes = iDirC.getAttributes("dns://8.8.8.8/www.google.com", new String[] {"A"});
            // get an enumeration of the attributes and print them out
            NamingEnumeration<?> attributeEnumeration = attributes.getAll();
            System.out.println("");
            while (attributeEnumeration.hasMore())
            {
                System.out.println("" + attributeEnumeration.next());
            }
            attributeEnumeration.close();
        }
        catch (UnknownHostException exception)
        {
            System.err.println("ERROR: Cannot access '" + host + "'");
        }
        catch (NamingException exception)
        {
            System.err.println("ERROR: No DNS record for '" + host + "'");
            exception.printStackTrace();
        }
    }
}

如果运行此代码,则会出现如下错误...

If I run this code, I get error like below...

www.google.com 74.125.128.103
ERROR: No DNS record for 'www.google.com'
javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name 'www.google.com'
    at com.sun.jndi.dns.DnsClient.query(DnsClient.java:300)
    at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
    at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:430)
    at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:231)
    at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:139)
    at com.sun.jndi.toolkit.url.GenericURLDirContext.getAttributes(GenericURLDirContext.java:103)
    at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142)
    at DNSLookup.main(DNSLookup.java:24)
Caused by: java.net.SocketTimeoutException: Receive timed out
    at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
    at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:121)
    at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
    at java.net.DatagramSocket.receive(DatagramSocket.java:786)
    at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:411)
    at com.sun.jndi.dns.DnsClient.query(DnsClient.java:203)
    ... 7 more

但是,如果我使用"dns:/www.google.com"作为查询,而不是"dns://8.8.8.8/www.google.com",则它可以完美运行而不会出现任何错误.仅当我尝试指定要使用的DNS服务器时,才会发生错误.

But if I use "dns:/www.google.com" as query, not "dns://8.8.8.8/www.google.com", it works perfectly without any error. Error only happens when I try to specify DNS server to use.

"dns://8.8.8.8/www.google.com"->错误

"dns://8.8.8.8/www.google.com" -> ERROR

"dns:/www.google.com"->工作!

"dns:/www.google.com" -> Work!

http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html

在本文档中,我要使用的功能完全相同.我不明白为什么会出问题.

In this document, there is exact same usase what I want to use. I cannot understand why this makes problem.

DirContext ictx = new InitialDirContext();
Attributes attrs3 = ictx.getAttributes("dns://server1.example.com/host3.example.com",
                                   new String[] {"MX"});

推荐答案

您的代码没有任何问题,但是我怀疑您是从阻止传出DNS的代理或防火墙后面运行它的.

There is nothing wrong with your code, but I suspect that you're running it from behind a proxy or firewall that blocks outgoing DNS.

当您要求提供 dns:/www.google.com 时,您实际上是在向本地DHCP提供的解析器询问 www.google.com 的IP-就像在浏览器地址栏中输入地址一样.

When you ask for dns:/www.google.com you're essentially asking your local DHCP-supplied resolver for the IP of www.google.com - just as when typing the address in the browser address line.

但是,当使用 dns://8.8.8.8/www.google.com 时,您要求使用Google DNS来解析 google.com ,这要求您的代理服务器/防火墙允许传出TCP/UDP端口53流量,这在公司环境中通常不是这种情况...

However, when using dns://8.8.8.8/www.google.com you're asking to use Google DNS to resolve google.com which requires that your proxy/firewall allows outgoing TCP/UDP port 53 traffic, which is usually not the case in corporate environments...

干杯

这篇关于Java JNDI DNS错误:“接收超时"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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