nslookup is a Unix utility that converts hostnames to IP addresses and IP addresses to hostnames. The following program emulates nslookup without any of nslookup's complex features.
Code:import java.io.*; import java.net.*; public class AddressResolver{ public static void main(String[] args){ AddressResolver.prompt(); BufferedReader buff = new BufferedReader( new InputStreamReader(System.in) ); String address = null; for(;;){ try{ address = buff.readLine(); }catch(IOException io){System.err.println(io);} if(address.equals("e")){ System.out.println("Thank you for using AddressResolver"); break; } InetAddress ina = null; try{ ina = InetAddress.getByName(address); }catch(UnknownHostException uhe){System.out.println(address + " is not a valid host"); AddressResolver.prompt(); continue; } if(address.endsWith(".com")){ System.out.println(address + " resolves to " + ina.getHostAddress()); AddressResolver.prompt(); }else{ System.out.println(address + " resolves to " + ina.getCanonicalHostName()); AddressResolver.prompt(); } } } private static void prompt(){ System.out.println(); System.out.println("Please enter an ip address or a url"); System.out.println("Press e to exit"); System.out.println(); } }




Reply With Quote