I have dual stack machine .
My problem is i am getting only IPv4 using
InetAddress address = InetAddress.getLocalHost();
and If i use Network Interface API then i get all the IP address in which includes my MAC addrres as well in the form of IP address. why-do-i-get-multiple-global-ipv6-addresses-listed-in-ifconfig
Now is there any way i can get both IPv4 and IPv6 of my machine.
In Linux, InetAddress.getLocalHost() will look for the hostname and then return the first IP address assigned to that hostname by DNS. If you have that hostname in the file /etc/hosts, it will get the first IP address in that file for that hostname.
If you pay attention this method returns only one InetAddress.
If you haven't assigned a hostname, most probably it will be localhost.localdomain. You can set the hostname with command line:
or by setting it in file /etc/sysconfig/network
If you want to get all ip addresses, including IPv6, assigned to a hostname you can use:
If you want to get all ip addresses, including IPv6, assigned to a host's network interfaces, you must use class NetworkInterface.
Here I'm pasting some example code:
For this example, I got code from one of the responses in InetAddress.getLocalHost().getHostAddress() is returning 127.0.1.1