On Linux RH dual stack machine InetAddress.getAllByName(hostname) is giving only IPv4 address. I tested with Linux RH7.1 with kernel 2.4.1.
How to reproduce it:
-------------------
1. Take the below pasted code and execute ..
import java.net.*;
import java.io.*;
public class GetIP {
public static void main(String[] arg)
{
try {
InetAddress[] iadr = InetAddress.getAllByName(arg[0]);
System.out.println("iadr length " + iadr.length);
System.out.println("host name " + arg[0]);
InetAddress iadrnic = InetAddress.getByName(arg[0]);
System.out.println("AFter getting iadr ");
NetworkInterface niface = NetworkInterface.getByAddr(iadrnic);
System.out.println("AFter getting niface");
System.out.println("network interface " + niface);
for(int i=0; i< iadr.length; i++)
{
System.out.println("ip address : " + iadr[i].getHostAddress());
System.out.println(" Host Name :" + iadr[i].getHostName());
//System.out.println("family " + iadr[i].getFamily());
System.out.println("Host aadd byte arr " + iadr[i].getAddress());
if(iadr[i] instanceof Inet4Address)
System.out.println("v4 address");
if(iadr[i] instanceof Inet6Address)
System.out.println("v6 address ");
if(!iadr[i].isMCOrgLocal()) {
System.out.println("NOt org local scope");
}
// InetAddress[] iadrtest = InetAddress.getAllByName(iadr[i].getHostName());
InetAddress iadrtest = InetAddress.getByName(iadr[i].getHostName());
System.out.println("working....");
}
} catch(Exception exp) {
System.out.println("exp mesg " + exp.getMessage());
exp.printStackTrace();
}
} // End of main
}
Execution output:
==================
java GetIP isolinear
iadr length 1
host name isolinear
AFter getting iadr
AFter getting niface
network interface name:lo (lo) index: 1 addresses:
/0:0:0:0:0:0:0:1;
/127.0.0.1;
ip address : 127.0.0.1
Host Name :isolinear
Host aadd byte arr [B@f62373
v4 address
NOt org local scope
working....
and ifconfig -a on same machine shows both the addresses.
======================
eth0 Link encap:Ethernet HWaddr 00:01:03:1A:7B:EF
inet addr:10.7.130.13 Bcast:10.7.130.255 Mask:255.255.255.0
inet6 addr: fe80::201:3ff:fe1a:7bef/10 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1829967 errors:0 dropped:0 overruns:81 frame:0
TX packets:2861824 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:355952814 (339.4 Mb) TX bytes:3302627646 (3149.6 Mb)
Interrupt:11 Base address:0xdc00
How to reproduce it:
-------------------
1. Take the below pasted code and execute ..
import java.net.*;
import java.io.*;
public class GetIP {
public static void main(String[] arg)
{
try {
InetAddress[] iadr = InetAddress.getAllByName(arg[0]);
System.out.println("iadr length " + iadr.length);
System.out.println("host name " + arg[0]);
InetAddress iadrnic = InetAddress.getByName(arg[0]);
System.out.println("AFter getting iadr ");
NetworkInterface niface = NetworkInterface.getByAddr(iadrnic);
System.out.println("AFter getting niface");
System.out.println("network interface " + niface);
for(int i=0; i< iadr.length; i++)
{
System.out.println("ip address : " + iadr[i].getHostAddress());
System.out.println(" Host Name :" + iadr[i].getHostName());
//System.out.println("family " + iadr[i].getFamily());
System.out.println("Host aadd byte arr " + iadr[i].getAddress());
if(iadr[i] instanceof Inet4Address)
System.out.println("v4 address");
if(iadr[i] instanceof Inet6Address)
System.out.println("v6 address ");
if(!iadr[i].isMCOrgLocal()) {
System.out.println("NOt org local scope");
}
// InetAddress[] iadrtest = InetAddress.getAllByName(iadr[i].getHostName());
InetAddress iadrtest = InetAddress.getByName(iadr[i].getHostName());
System.out.println("working....");
}
} catch(Exception exp) {
System.out.println("exp mesg " + exp.getMessage());
exp.printStackTrace();
}
} // End of main
}
Execution output:
==================
java GetIP isolinear
iadr length 1
host name isolinear
AFter getting iadr
AFter getting niface
network interface name:lo (lo) index: 1 addresses:
/0:0:0:0:0:0:0:1;
/127.0.0.1;
ip address : 127.0.0.1
Host Name :isolinear
Host aadd byte arr [B@f62373
v4 address
NOt org local scope
working....
and ifconfig -a on same machine shows both the addresses.
======================
eth0 Link encap:Ethernet HWaddr 00:01:03:1A:7B:EF
inet addr:10.7.130.13 Bcast:10.7.130.255 Mask:255.255.255.0
inet6 addr: fe80::201:3ff:fe1a:7bef/10 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1829967 errors:0 dropped:0 overruns:81 frame:0
TX packets:2861824 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:355952814 (339.4 Mb) TX bytes:3302627646 (3149.6 Mb)
Interrupt:11 Base address:0xdc00