// SClient0.java import java.io.*; import java.net.*; public class SClient0 { /** * The main entry point for the application. * * @param args Array of parameters passed to the application * via the command line. */ public static boolean debugging = true; public static void main (String[] args) throws IOException { printLocalAddress(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { String name; do { System.out.print("Enter a hostname or IP address: "); System.out.flush(); name = in.readLine(); if (name != null) printRemoteAddress(name); } while (name != null); System.out.println("exit"); } catch(IOException ex) { System.out.println("Input error:"); ex.printStackTrace(); } dbgPause(); } static void printLocalAddress() { try { InetAddress myself = InetAddress.getLocalHost(); System.out.println("My name : " + myself.getHostName()); System.out.println("My IP : " + myself.getHostAddress()); } catch (UnknownHostException ex) { System.out.println("Failed to find my local address:"); ex.printStackTrace(); } } static void printRemoteAddress(String name) { try { System.out.println("Looking up " + name + "..."); InetAddress machine =InetAddress.getByName(name); System.out.println("Host name : " + machine.getHostName()); System.out.println("Host IP : " + machine.getHostAddress() ); System.out.println("Now... use getAllByName()"); InetAddress names[] = InetAddress.getAllByName(name); for (int i = 0; i < names.length; i++ ) { System.out.println("Host name: " + names[i].getHostName()); System.out.println("Host IP : " + names[i].getHostAddress()); } } catch ( UnknownHostException ex) { System.out.println("Failed to lookup " + name); ex.printStackTrace(); } } protected static void dbgPause()throws IOException { if (debugging) System.in.read(); } }