JDK-6210227 : REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_9,windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2004-12-17
  • Updated: 2018-12-18
  • Resolved: 2005-03-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u3 b05Fixed 6Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux unununium 2.4.21-20.EL #1 Wed Aug 18 20:58:25 EDT 2004 i686 i686 i386 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
Calling Socket.getLocalAddress() used to return the IP address of the interface over which the connection to the remote was was being made.  Now it alway returns 0.0.0.0.  We have written a sample program which demonstrates the bug.  The program works with jre 1.4.2, but not with 5.0.  This problem seems to be a linux only problem.  It works fine on Windows.  We did try it on both red hat linux 7.3 and Red Hat Enterprise Linux 3.0, and it failed on both of those.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test program provided:

usage: java Interfaceid <hostname> where <hostname> is a website such as google.com

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
$ java Interfaceid google.com
get ip and hostname of our interface to google.com
local ip is 192.168.100.53
local hostname is curium.avamar.com

ACTUAL -
$ java Interfaceid google.com
get ip and hostname of our interface to google.com
local ip is 0.0.0.0
local hostname is 0.0.0.0


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;
import java.net.*;

public class Interfaceid {
 
public static void main(String[] args) {
            if( null == args[0] ) {
                        System.out.println("usage: java Interfaceid <hostname> where <hostname> is a website such as google.com");
            } else {
                        String sHostname = args[0];
            System.out.println("get ip and hostname of our interface to "+sHostname);
            try {
                        InetAddress ina = InetAddress.getByName( sHostname );
                        InetSocketAddress isa = new InetSocketAddress( ina, 80 );
                Socket s = new Socket();
                s.connect( isa, 1000 );
                InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue
                String      sLocalHostname = iaLocal.getHostName();
                System.out.println("local ip is "+iaLocal.getHostAddress() );
                System.out.println("local hostname is "+sLocalHostname );
            } catch(Exception e) {
                        System.out.println("Exception happened");
            }
        }
}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
We have not been able to find any workaround that will give us the IP address of the NIC being used for the connection on a dual NIC machine.  We also tried using Socket.getLocalSocketAddress() , and this failed in the same way.  We presume that the same underlying code is used to get the IP address.

Release Regression From : 1.4.2
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.
###@###.### 2004-12-17 21:52:13 GMT

Comments
WORK AROUND A work-around would be to bind the socket to a local address (even 0.0.0.0) before calling connect() and then you will get the correct local address from getLocalAddress() ###@###.### 2004-12-20 22:41:09 GMT
20-12-2004

EVALUATION This was introduced by the new code for IPv6 on Windows. The problem is that the second file-descriptor which is used on Windows but not Linux/Solaris is being cleared correctly when the socket is bound, but not when connect is called without being bound. ###@###.### 2004-12-20 22:41:09 GMT
20-12-2004