Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4193642

InetAddress.getByName does not return a name

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.2.0
    • 1.1.6, 1.1.7
    • core-libs
    • 1.2
    • generic, x86, sparc
    • generic, solaris_2.5.1, windows_nt
    • Not verified

      The following program demonstrates a problem where Java will not return a hostname. This happens when requests are made using different IP addresses in calls for the (same) hostname (the host has multiple IP addresses).

      java.net.*;

      public class GetAllByName {
          void doit(String org, InetAddress test) {
              System.out.println("Created by " + org);
              System.out.println("InetAddress.toString() = " + test.toString());
              String name = test.getHostName();
              System.out.println("name = " + name.toString());
              System.out.println("");
          }

          public static void main (String[] args) {
              InetAddress test;
              GetAllByName hn = new GetAllByName();
              String addr = new String("172.29.252.66");
              String addr2 = new String("172.29.252.34");
              String addr3= new String("172.29.252.50");
              String addr4= new String("172.29.252.58");
              String name = new String("sb-4500-1");

              int i;
              InetAddress [] all;

              try {
                  all = InetAddress.getAllByName(name);

                  System.out.println("getAllByName(" + name + ") found " +
                                     all.length + " addresses");
                  for (i = 0; i < all.length; ++i) {
                      hn.doit(name, all[i]);
                  }
              } catch (Exception ex) {
                  System.out.println("Caught exception = " + ex);
              }

              try {
                  all = InetAddress.getAllByName(addr);

                  System.out.println("getAllByName(" + addr + ") found " +
                                     all.length + " addresses");

                  for (i = 0; i < all.length; ++i) {
                      hn.doit(addr, all[i]);
                  }
              } catch (Exception ex) {
                  System.out.println("Caught exception = " + ex);
              }

              try {
                  all = InetAddress.getAllByName(addr2);

                  System.out.println("getAllByName(" + addr2 + ") found " +
                                     all.length + " addresses");

                  for (i = 0; i < all.length; ++i) {
                      hn.doit(addr2, all[i]);
                  }
              } catch (Exception ex) {
                  System.out.println("Caught exception = " + ex);
              }

              try {
                  all = InetAddress.getAllByName(addr3);

                  System.out.println("getAllByName(" + addr3 + ") found " +
                                     all.length + " addresses");

                  for (i = 0; i < all.length; ++i) {
                      hn.doit(addr3, all[i]);
                  }
              } catch (Exception ex) {
                  System.out.println("Caught exception = " + ex);
              }

              try {
                  all = InetAddress.getAllByName(addr4);

                  System.out.println("getAllByName(" + addr4 + ") found " +
                                     all.length + " addresses");

                  for (i = 0; i < all.length; ++i) {
                      hn.doit(addr4, all[i]);
                  }
              } catch (Exception ex) {
                  System.out.println("Caught exception = " + ex);
              }
          }
      }
      ++++++++++++++++++++++++++++++++++++++++++++++++++++
      The results of execution:

      Created by sb-4500-1
      name = sb-4500-1

      Created by 172.29.252.66
      name = sb-4500-1.cisco.com

      Created by 172.29.252.34
      name = 172.29.252.34

      Created by 172.29.252.50
      name = 172.29.252.50

      Created by 172.29.252.58
      name = 172.29.252.58

      elizabeth.mezias@Eng 1998-11-30

            ywangsunw Yingxian Wang (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: