2011-06-24 46 views
3

我想从使用JNDI的DNS服务器获取SRV记录。使用JNDI获取DNS SRV记录

Hashtable<String, String> env = new Hashtable<String, String>(); 
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); 
env.put("java.naming.provider.url", "dns://dns.server.com"); 
DirContext ctx = new InitialDirContext(env); 
Attributes attributes = ctx.getAttributes("_sip._udp", new String [] { "SRV" }); 
return attributes; 

但试图让我的属性得到了以下异常时

DNS错误[根异常是 java.net.PortUnreachableException: ICMP端口不可达]。剩余名称 '_sip._udp'

我已经验证主机-t SRV _sip._udp.server.com返回有效的SRV记录。

为什么会发生这种情况的任何原因?

回答

4

下列其中一项:dns.server.com不是有效的DNS服务器,没有_sip._udp的SRV记录,DNS服务不响应端口53(标准DNS端口)或您的Java代码错误。

要诊断DNS服务器故障,您可以尝试使用host -t SRV _sip._udp.server.com dns.server.comdig @dns.server.com -t SRV _sip._udp.server.com来确认服务器是否正常工作。

如果host或返回预期的条目,请尝试以下更改代码:

变化:

env.put("java.naming.provider.url", "dns://dns.server.com"); 

要:

env.put("java.naming.provider.url", "dns:"); 

(即只使用您的操作系统的标准DNS解析)

更改:

ctx.getAttributes("_sip._udp", new String [] { "SRV" }); 

要:

ctx.getAttributes("_sip._udp.domain.com", new String [] { "SRV" }); 

为SRV记录需要域名进行搜索,所以你结束:

Hashtable<String, String> env = new Hashtable<String, String>(); 
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); 
DirContext ctx = new InitialDirContext(env); 
Attributes attributes = ctx.getAttributes("_sip._udp.domain.com", new String [] { "SRV" }); 
return attributes;