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

java/net/InetAddress.getByName() does not always return the expected address typ

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp



      Name: dk106046 Date: 08/26/2004

      OPERATING SYSTEM(S):
      Windows XP

      FULL JDK VERSION(S):
      java version "1.5.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta3-b56)
      Java HotSpot(TM) Client VM (build 1.5.0-beta3-b56, mixed mode)


      According to the Networking IPv6 User Guide, IPv4 addresses should be preferred
      over IPv6 addresses, unless java.net.preferIPv6Addresses is set to 'true'. This
      does not happen in the following scenario:

      Add these lines to c:\windows\system32\drivers\etc\hosts:

      fe80:0:0:0:20d:60ff:fe49:47d TestHost
      9.20.187.96 TestHost
      9.20.187.06 TestHost
      fe80:0:0:0:20d:60ff:fe49:47 TestHost

      Compile and run the following test:

      public class test {
          public static void main (String [] args) {
              try {
                  System.out.println("Using getByName(): "
                      + java.net.InetAddress.getByName(args.length < 1 ? "localhost" : args[0]).getHostAddress());
                  java.net.InetAddress[] ias = java.net.InetAddress.getAllByName(args.length < 1 ? "localhost" : args[0]);
                  System.out.println("Using getAllByName():");
                  for (int i=0; i<ias.length; i++) {
                      String getbyName = ias[i].getHostAddress();
                      System.out.println(" " + getbyName);
                  }
              } catch (Exception ex) {
                  System.out.println("Exc: " + ex.getMessage());
                  ex.printStackTrace();
              }
          }
      }

      C:\dev\scratch>java test TestHost
      Using getByName(): fe80:0:0:0:20d:60ff:fe49:47
      Using getAllByName():
          fe80:0:0:0:20d:60ff:fe49:47
          9.20.187.6
          9.20.187.96
          fe80:0:0:0:20d:60ff:fe49:47d

      C:\dev\scratch>java -Djava.net.preferIPv6Addresses
      =true test TestHost
      Using getByName(): fe80:0:0:0:20d:60ff:fe49:47d
      Using getAllByName():
          fe80:0:0:0:20d:60ff:fe49:47d
          9.20.187.96
          9.20.187.6
          fe80:0:0:0:20d:60ff:fe49:47

      C:\dev\scratch>java -Djava.net.preferIPv4Stack=true test TestHost
      Using getByName(): 9.20.187.96
      Using getAllByName():
          9.20.187.96
          9.20.187.6

      The first invocation should have resulted in the IPv4 address returned by the last
      invocation, where IPv6 is disabled. The last invocation produces the same result
      with 1.4.2. ("Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)").

      [This bug is being submitted as a courtesy, in order to maintain uniformity between Sun & IBM JDKs. It has been fixed in IBM JDKs.
      Please contact ###@###.### if you have questions.]
      ======================================================================

            Unassigned Unassigned
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: