0

I have a dual stack Linux machine that supports both IPv4 and IPv6 . Now my query is when ever i tried to call InetAddress.getLocalHost() it always gives me IPv4.

Can i have both IPv4 and IPv6 in InetAddress.getLocalHost() response . If not then how will i get IPv6 address from InetAddress.getLocalHost().

T-Bag
  • 10,916
  • 3
  • 54
  • 118

1 Answers1

1

I responded a similar question here: How to fetch both IPv4 and IPv6 on dual stack OS

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.

You can use the code in the link indicated (How to fetch both IPv4 and IPv6 on dual stack OS) and then use function getIPv6Addresses in the link indicated by @Rafiq (Return IPv6 in Java) to obtain only the IPv6 addresses.

I hope it helps.

Community
  • 1
  • 1
rodolk
  • 5,606
  • 3
  • 28
  • 34