Name: dgC58589 Date: 01/20/98
The InetAddress returned by Socket.getLocalAddress
returns the IP address in a reverse order.
The bug is present on Solaris x86 1.1.5 and
all tested versions of the Linux JDK.
The sparc version works correctly.
Source code follows:
import java.net.*;
import java.io.*;
public
class inet
{
public static void main(String argv[])
{
if (argv.length < 2)
{
System.err.println("Usage : java inet <host> <port>");
System.exit(-1);
}
int port = 0;
String host = argv[0];
try
{
port = Integer.parseInt(argv[1]);
}
catch (NumberFormatException e)
{
System.err.println("<port> must be a number!");
System.err.println("Usage : java inet <host> <port>");
System.exit(-1);
}
try
{
Socket sock = new Socket(host,port);
InetAddress in = sock.getLocalAddress();
InetAddress in2 = InetAddress.getByName(host);
System.out.println("Socket.getLocalAddress returns (wrong) : " + in);
System.out.println("InetAddress.getByName returns (right) : " + in2);
}
catch (UnknownHostException e1)
{
System.err.println(e1);
System.err.println("Usage : java inet <host> <port>");
System.exit(-1);
}
catch (IOException e2)
{
e2.printStackTrace(System.err);
}
}
}
(Review ID: 23574)
======================================================================
- duplicates
-
JDK-4106601 java.net.Socket.getLocalAddress() works wrong on Solaris x86
- Closed