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]
======================================================================
- relates to
-
JDK-4056768 InetAddress.getLocalHost() always returns "localhost"
- Closed
-
JDK-4092931 DatagramSocket.getLocalAddress() returns incorrect IP InetAddress info.
- Closed