JDK-4065656 : ServerSocket.getInetAddress() returns incorrect IP address.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.1.3
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.5.1,windows_nt
  • CPU: x86,sparc
  • Submitted: 1997-07-18
  • Updated: 1998-02-13
  • Resolved: 1998-02-13
Related Reports
Relates :  
Relates :  
Description

Name: rlT66838			Date: 07/18/97


ServerSocket.getInetAddress() returns a different value
depending on how it was constructed.  If it was constructed
with an explicit InetAddress then it returns the correct
value.  If the InetAddress is not specified then the value
returned is "0.0.0.0", when it should instead contain the
host's IP address.  This is even more complicated on
multi-home machines where it is less obvious to which IP
address the socket is bound (it should be the one returned
by InetAddress.getLocalHost())

The following test program illustrates the problem.
Compile and run with no arguments.

import java.io.*;
import java.net.*;
 
public class Test {
  public static void main(String[] args) {
    try {
      ServerSocket server = new ServerSocket(0);
      System.out.println("default address: " + server);
      server = new ServerSocket(0, 50, InetAddress.getLocalHost());
      System.out.println("explicit address: " + server);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
}

The output on my machine is:

default address: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=53261]
explicit address: ServerSocket[addr=adams/206.64.15.42,port=0,localport=53262]

The output should have been:

default address: ServerSocket[addr=adams/206.64.15.42,port=0,localport=53261]
explicit address: ServerSocket[addr=adams/206.64.15.42,port=0,localport=53262]


======================================================================

Comments
WORK AROUND Name: rlT66838 Date: 07/18/97 When creating a ServerSocket always pass it InetAddress.getLocalHost() when binding to the "default" ip address. This does not work with JDK 1.0.2 which has no such constructor for ServerSocket. ======================================================================
11-06-2004

EVALUATION 0.0.0.0 is the correct address to bind to for servers with multiple homes. We may add a constant called InAddrAny for testing. benjamin.renaud@Eng 1998-02-12
12-02-1998