📄 udpnslookup.java
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import java.net.*;
import java.util.*;
public class UDPNSLookup {
public static void main (String[] args) {
if (args.length != 1)
throw new IllegalArgumentException
("Syntax: UDPNSLookup <hostname>[@<nameserver>]");
int atIdx = args[0].indexOf ("@");
String nameServer = (atIdx > -1) ? args[0].substring (atIdx + 1) : "ns";
String hostName = (atIdx > -1) ? args[0].substring (0, atIdx) : args[0];
System.out.println ("Nameserver: " + nameServer);
System.out.println ("Request: " + hostName);
DNSQuery query = new DNSQuery (hostName, DNS.TYPE_ANY, DNS.CLASS_IN);
try {
boolean received = false;
int count = 0;
DatagramSocket socket = new DatagramSocket ();
socket.setSoTimeout (5000);
try {
while (!received) {
try {
sendQuery (query, socket, InetAddress.getByName (nameServer));
getResponse (query, socket);
received = true;
} catch (InterruptedIOException ex) {
if (count ++ < 3) {
System.out.println ("resend..");
} else {
throw new IOException ("No response received from nameserver");
}
}
}
} finally {
socket.close ();
}
NSLookup.printRRs (query);
} catch (IOException ex) {
System.out.println (ex);
}
}
public static void sendQuery (DNSQuery query, DatagramSocket socket, InetAddress nameServer) throws IOException {
byte[] data = query.extractQuery ();
DatagramPacket packet = new DatagramPacket
(data, data.length, nameServer, DNS.DEFAULT_PORT);
socket.send (packet);
}
public static void getResponse (DNSQuery query, DatagramSocket socket) throws IOException {
byte[] buffer = new byte[512];
DatagramPacket packet = new DatagramPacket (buffer, buffer.length);
socket.receive (packet);
query.receiveResponse (packet.getData (), packet.getLength ());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -