我正在使用dnsjava进行查询,程序确实查询了2次,第一次查询是必需的并返回所需的结果(此结果仅看到我是否捕获UDP包,而不是我的程序的结果(,之后,它会自动执行第二次查询并返回错误 找不到我的程序的主机 tcpdump_picture
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
final Cache cache = new Cache(DClass.IN);
SimpleResolver resolver = new SimpleResolver("10.212.9.108");
Name name = new Name("8491698766.6.msisdn.sub.cs");
Lookup.setDefaultResolver(resolver);
Lookup.setDefaultCache(new Cache(), DClass.IN);
Lookup l = new Lookup(name);
l.setResolver(resolver);
l.run();
Record[] records = l.getAnswers();
System.out.println("Error str: " + l.getErrorString());
// System.out.println("Record length: " + records.length);
// InetAddress[] array = new InetAddress[records.length];
if(records != null) {
for(int i=0; i<records.length; i++) {
Record record = records[i];
if(record instanceof ARecord) {
ARecord a = (ARecord)record;
System.out.println("ARecord: " + a.getAddress().getHostName() + ";" + a.getAddress().getHostAddress());
}else {
CNAMERecord cname = (CNAMERecord)record;
System.out.println("CnameRecord: " + cname.toString());
}
}
}else {
System.out.println("Record null");
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
您只需要在查询标头中取消设置Recursion desired
标志。
假设您有一个具有区域bob.
和以下条目的 DNS 服务器:
peter.bob. 38400 IN NS ns2.bobsen-technology.org.
peter.bob. 38400 IN NS ns.klaus.de.
代码
Resolver resolver = new SimpleResolver("localhost");
Record question = Record.newRecord(new Name("peter.bob."), Type.NS, DClass.IN);
Message query = Message.newQuery(question);
query.getHeader().unsetFlag(Flags.RD);
Message response = resolver.send(query);
for (RRset rRset : response.getSectionRRsets(Section.AUTHORITY)) {
System.out.println(rRset);
}
生产:
{ peter.bob. 38400 IN NS [ns2.bobsen-technology.org.] [ns.klaus.de.] }