modified DNSqueries.java, added loop and some comment
authorHansjürg Wenger <hansjuerg.wenger@bfh.ch>
Tue, 14 May 2013 10:41:15 +0000 (10:41 +0000)
committerHansjürg Wenger <hansjuerg.wenger@bfh.ch>
Tue, 14 May 2013 10:41:15 +0000 (10:41 +0000)
DNSqueries.java

index 7be72bb..402d9b8 100644 (file)
@@ -1,61 +1,79 @@
 /*
- * (c) 2010 WGH1@BFH
+ * (c) 2010-2013 WGH1@BFH
  *
- * shows how to use the DNS resolver library within java
+ * demo program to show the use of the java hostname resolver
  *
  */
 
-// Import the Scanner class
+// import the Scanner class
 import java.util.Scanner;
-// Import the InetAddress class
+// import the InetAddress class
 import java.net.InetAddress;
-// Import the UnknownHostException class
+// import the UnknownHostException class
 import java.net.UnknownHostException;
 
 public class DNSqueries {
 
-       public static void main(String[] args) {
+  public static void main(String[] args) {
                 
-               try {
-                       int i;
-                       Scanner s;
-                       // Internet Address
-                       InetAddress iAddr;
-                       // Array of Internet Addresses
-                       InetAddress[] iList;
-                       // Hostnames
-                       String inHostName, canHostName;
-
-                       // read in hostname or IP address 
-                       s = new Scanner(System.in);
-                       System.out.print("\nEnter Hostname or IP-Address: ");
-                       inHostName = s.next();
-
-                       // Determines the IP address of a host, given the host's name
-                       iAddr = InetAddress.getByName( inHostName );
-
-                       // Gets the fully qualified domain name for this IP address
-                       canHostName = iAddr.getCanonicalHostName();
-
-                       // Given the name of a host, returns an array of its IP addresses,
-                       // based on the configured name service on the system
-                       iList = InetAddress.getAllByName( canHostName );
-
-                       // Print out the FQDN
-                       System.out.println("\nCanonical Hostname: " + iAddr.getCanonicalHostName() + "\n");
-
-                       // Print out ..
-                       for (i=0;i<iList.length;i++) {
-                               //                 .. FQDN ..
-                               System.out.println("Hostname " + i + ": " + iList[i].getCanonicalHostName() +
-                               //                 .. and address
-                                                  "  Address " + i + ": " + iList[i].getHostAddress() );
-                       }
-                       System.out.println();
-               }
-               // Exception handler
-               catch (UnknownHostException exception) {
-                       System.err.println("\nHost unknown: " + exception.getMessage() + "\n");
-               }
-       }
+    // do not cache DNS responses inside java
+    java.security.Security.setProperty("networkaddress.cache.ttl" , "0");
+    java.security.Security.setProperty("networkaddress.cache.negative.ttl" , "0");
+
+    // iteration variable
+    int i;
+    // input scanner
+    Scanner s;
+    // internet address
+    InetAddress iAddr;
+    // array of internet addresses
+    InetAddress[] iList;
+    // hostnames
+    String inHostName, canHostName;
+
+    // new scanner 
+    s = new Scanner(System.in);
+
+    // loop until "exit"
+    do {
+      // catch exceptions
+      try {
+        // read in hostname or IP address
+        System.out.print("\nEnter Hostname or IP-Address (exit to quit): ");
+        inHostName = s.next();
+
+        // exit if "hostname" is "exit"
+        if (inHostName.equals("exit")) {
+          System.out.println("\nGood Bye!\n");
+          break;
+        }
+
+        // determines the IP address of a host, given the host's name
+        iAddr = InetAddress.getByName( inHostName );
+
+        // gets the fully qualified domain name for this IP address
+        canHostName = iAddr.getCanonicalHostName();
+
+        // given the name of a host, returns an array of its IP addresses,
+        // based on the configured name services on the system
+        iList = InetAddress.getAllByName( canHostName );
+
+        // print out the FQDN
+        System.out.println("\nCanonical Hostname: " + iAddr.getCanonicalHostName() + "\n");
+
+        // print out ..
+        for (i=0;i<iList.length;i++) {
+          //                 .. FQDN ..
+          System.out.println("Hostname " + i + ": " + iList[i].getCanonicalHostName() +
+          //                 .. and address
+                             "  Address " + i + ": " + iList[i].getHostAddress() );
+        }
+      }
+      // exception handler
+      // handle unknown host exception
+      catch (UnknownHostException exception) {
+        System.err.println("\nHost unknown: " + exception.getMessage() + "\n");
+      }
+    } while (true); // loop ..
+  }
 }