The following program demonstrates a problem where Java will not return a hostname. This happens when requests are made using different IP addresses in calls for the (same) hostname (the host has multiple IP addresses).
java.net.*;
public class GetAllByName {
void doit(String org, InetAddress test) {
System.out.println("Created by " + org);
System.out.println("InetAddress.toString() = " + test.toString());
String name = test.getHostName();
System.out.println("name = " + name.toString());
System.out.println("");
}
public static void main (String[] args) {
InetAddress test;
GetAllByName hn = new GetAllByName();
String addr = new String("172.29.252.66");
String addr2 = new String("172.29.252.34");
String addr3= new String("172.29.252.50");
String addr4= new String("172.29.252.58");
String name = new String("sb-4500-1");
int i;
InetAddress [] all;
try {
all = InetAddress.getAllByName(name);
System.out.println("getAllByName(" + name + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(name, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr);
System.out.println("getAllByName(" + addr + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr2);
System.out.println("getAllByName(" + addr2 + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr2, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr3);
System.out.println("getAllByName(" + addr3 + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr3, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr4);
System.out.println("getAllByName(" + addr4 + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr4, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++
The results of execution:
Created by sb-4500-1
name = sb-4500-1
Created by 172.29.252.66
name = sb-4500-1.cisco.com
Created by 172.29.252.34
name = 172.29.252.34
Created by 172.29.252.50
name = 172.29.252.50
Created by 172.29.252.58
name = 172.29.252.58
elizabeth.mezias@Eng 1998-11-30
java.net.*;
public class GetAllByName {
void doit(String org, InetAddress test) {
System.out.println("Created by " + org);
System.out.println("InetAddress.toString() = " + test.toString());
String name = test.getHostName();
System.out.println("name = " + name.toString());
System.out.println("");
}
public static void main (String[] args) {
InetAddress test;
GetAllByName hn = new GetAllByName();
String addr = new String("172.29.252.66");
String addr2 = new String("172.29.252.34");
String addr3= new String("172.29.252.50");
String addr4= new String("172.29.252.58");
String name = new String("sb-4500-1");
int i;
InetAddress [] all;
try {
all = InetAddress.getAllByName(name);
System.out.println("getAllByName(" + name + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(name, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr);
System.out.println("getAllByName(" + addr + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr2);
System.out.println("getAllByName(" + addr2 + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr2, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr3);
System.out.println("getAllByName(" + addr3 + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr3, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
try {
all = InetAddress.getAllByName(addr4);
System.out.println("getAllByName(" + addr4 + ") found " +
all.length + " addresses");
for (i = 0; i < all.length; ++i) {
hn.doit(addr4, all[i]);
}
} catch (Exception ex) {
System.out.println("Caught exception = " + ex);
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++
The results of execution:
Created by sb-4500-1
name = sb-4500-1
Created by 172.29.252.66
name = sb-4500-1.cisco.com
Created by 172.29.252.34
name = 172.29.252.34
Created by 172.29.252.50
name = 172.29.252.50
Created by 172.29.252.58
name = 172.29.252.58
elizabeth.mezias@Eng 1998-11-30
- relates to
-
JDK-4070612 InetAddress will not randomize multiple IP addresses.
- Closed