FileDocCategorySizeDatePackage
HostLookup.javaAPI DocExample1795Sun Dec 12 10:51:40 GMT 2004None

HostLookup

public class HostLookup extends Object

Fields Summary
Constructors Summary
Methods Summary
private static booleanisHostname(java.lang.String host)


    // Is this an IPv6 address?
    if (host.indexOf(':") != -1) return false;
      
    char[] ca = host.toCharArray();
    // if we see a character that is neither a digit nor a period
    // then host is probably a hostname
    for (int i = 0; i < ca.length; i++) {
      if (!Character.isDigit(ca[i])) {
        if (ca[i] != '.") return true;
      }
    }

    // Everything was either a digit or a period
    // so host looks like an IPv4 address in dotted quad format
    return false;

   
private static java.lang.Stringlookup(java.lang.String host)


    InetAddress node;

    // get the bytes of the IP address
    try {
      node = InetAddress.getByName(host);
    }
    catch (UnknownHostException ex) {
      return "Cannot find host " + host;
    }

    if (isHostname(host)) {
      return node.getHostAddress();
    }
    else {  // this is an IP address
      return node.getHostName();
    }

  
public static voidmain(java.lang.String[] args)


    if (args.length > 0) { // use command line
      for (int i = 0; i < args.length; i++) {
        System.out.println(lookup(args[i]));
      }
    }
    else {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter names and IP addresses. Enter \"exit\" to quit.");
      try {
        while (true) {
          String host = in.readLine();
          if (host.equalsIgnoreCase("exit") || host.equalsIgnoreCase("quit")) {
            break;
          }
          System.out.println(lookup(host));
        }
      }
      catch (IOException ex) {
        System.err.println(ex);
      }

   }