Name: mf23781 Date: 07/06/98
The javadoc for Java 1.1.6 specifies for
InetAddress.getHostName: public String getHostName()
Returns the fully qualified host name for this address.
If the host is equal to null, then this address refers to any of
the local machine's available network addresses.
Returns: the fully qualified host name for this address.
However, it does not seem to be returning the fully qualified name in all
cases. A sample java application:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.out.println("InetAddressTest <hostname>");
return;
}
InetAddress localhost;
InetAddress host;
try
{
localhost = InetAddress.getLocalHost();
host = InetAddress.getByName(args[0]);
}
catch (UnknownHostException e)
{
System.out.println("Unknown host exception: " + e.getMessage());
return;
}
System.out.println("local host:");
System.out.println(" Fully qualified name is \"" +
localhost.getHostName() + "\"");
System.out.println(" IP address is " + localhost.getHostAddress());
System.out.println(args[0] + ":");
System.out.println(" Fully qualified name is \"" +
host.getHostName() + "\"");
System.out.println(" IP address is " + host.getHostAddress());
}
}
Here's a sample run:
.../java/test> hostname
lambda.rchland.ibm.com
.../java/test> java InetAddressTest lambda
local host: Fully qualified name is "lambda.rchland.ibm.com" IP address is 9.5.55.52
lambda:
Fully qualified name is "lambda" IP address is 9.5.55.52
Note that the second InetAddress (host) is not producing a fully qualified
name.
Further investigation here at Hursley discovered the following.
If an InetAddress object is created with a machine name (i.e.
java.sun.com) the address is looked up and the hostname stored.
This is then returned _as is_ by getHostName (i.e. if you
do InetAddress.getByName("lambda") "lambda" is what you get back).
However, if you create an InetAddress with an address string
(i.e. "1.2.3.4") getHostName does a reverse lookup to get the
name. This should be fully qualified.
Therefore, a workaround is to create an InetAddress object using
the machine name, get it's address and create a second InetAdress
object. This should return the fully qualified name :
import java.net.*;
public class InetAddressTest {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("InetAddressTest <hostname>");
return;
}
InetAddress host;
InetAddress host2;
try {
host = InetAddress.getByName(args[0]);
host2 = InetAddress.getByName(host.getHostAddress());
} catch (UnknownHostException e) {return;};
System.out.println(args[0] + ":");
System.out.println(" Fully qualified name is " + host2.getHostName());
System.out.println(" IP address is " + host2.getHostAddress());
}
}
Sample run:
aberfeldy.hursley.ibm.com: java InetAddressTest aberfeldy
aberfeldy:
Fully qualified name is aberfeldy.hursley.ibm.com
IP address is 9.20.24.73
It may be that this behaviour is deliberate as changing Java to
make InetAddress.getHostName return a fully qualified host name
will require that this call makes a network call that would
cause a hang if the server at the other end is not connected.
If this is the case then the documentation should be changed
as it is misleading.
======================================================================
Unfortunately, the workaround doesn't work. Running the test program above, here's what I get:
mspivak@apilon:~/tmp% java InetAddressTest apilon
apilon:
Fully qualified name is apilon
IP address is 129.144.252.102
As far as I know, there's still no reliable way to get an FQDN.
max.spivak@Eng 1998-08-10
======================================================================