package nslookup; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class Nslookup { /** * @param args */ public static void main(String[] args) { if(args.length == 0) { System.out.println("Usage is:\njava Nslookup nameserver hostname [true!false]"); return; } String serverName = args[0]; String hostName = args[1]; boolean cached = Boolean.parseBoolean(args[2]); InetAddress sa = null; try { System.out.println("Ricerca indirizzo per NameServer su "+serverName); sa = InetAddress.getByName(serverName); System.out.println("NameServer:"+sa); } catch (UnknownHostException e) { System.out.println("Host "+serverName+" sconosciuto"); return; } Socket s = null; try { s = new Socket(sa,NameServer.NAMESERVERPORT); } catch (IOException e) { System.out.println("Errore nella connessione con il NameServer"); return; } ObjectOutputStream oos; try { oos = new ObjectOutputStream(s.getOutputStream()); } catch (IOException e) { System.out.println("Errore nella creazione dell'ObjectOutputStream"); return; } NameQuery nq = new NameQuery(hostName,cached); long t0 = System.currentTimeMillis(); try { oos.writeObject(nq); } catch (IOException e) { System.out.println("Errore nell'invio della richiesta"); return; } ObjectInputStream ois = null; try { ois = new ObjectInputStream(s.getInputStream()); } catch (IOException e) { System.out.println("Errore nella creazione dell?ObjectInputStream"); } QueryAnswer qa = null; long t1 = 0L; try { qa = (QueryAnswer) ois.readObject(); t1 = System.currentTimeMillis(); } catch (IOException e) { System.out.println("Errore nella lettura della risposta"); return; } catch (ClassNotFoundException e) { System.out.println("Errore: tipo sconosciuto dell'oggetto letto"); return; } System.out.println("NsLookup "+hostName+":"+qa.getInetAddress()); System.out.println("Impiegati "+(t1-t0)+" msec"); } }