JDK-5092451 : java/net/InetAddress.getByName() does not always return the expected address typ
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-08-26
  • Updated: 2004-08-26
  • Resolved: 2004-08-26
Related Reports
Duplicate :  

Name: dk106046			Date: 08/26/2004

Windows XP

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                   TestHost                   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());

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

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

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

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.]

WORK AROUND Name: dk106046 Date: 08/26/2004 Modify the order of entries in the c:\windows\system32\drivers\etc\hosts file. ======================================================================

EVALUATION This is duplicate of 5087907. It was found too late for tiger but has been fixed for mustang. The correct behaviour for when java.net.preferIPv6Addresses=false (default setting) is for the IPv4 addresses to appear before the IPv6 addresses in the returned list. Note this isn't the same behaviour as when IPv6 is disabled. When IPv6 is disabled only IPv4 addresses are returned. ###@###.### 2004-08-26