network-programmingsocketchannel

Getting ip6 format in getInetAddress()


Will getInetAddress() from a socket return ip in ip4 or ip6 format?

I have been receiving ip6 format only for localhost. Are there any machine level changes to address this?


Solution

  • Will getInetAddress() from a socket return ip in ip4 or ip6 format?

    It will simply return the remote address the socket is connected to, whatever that is. If the socket is a TCP/IPv6 socket, the address will be an IPv6 address. If the socket is a TCP/IPv4 socket, the address will be an IPv4 address.

    Note, you are talking about "IPv4 format" and "IPv6 format" addresses. That is highly misleading. IPv4 and IPv6 are not just different formats of representing IP addresses. They are completely different network protocols. You are either using the IPv4 protocol, and then you are using IPv4 addresses, or you are using the IPv6 protocol, and then you are using IPv6 addresses.

    I have been receiving ip6 format only for localhost. Are there any machine level changes to address this?

    No. This is a property of the connection, not the machine. If you want an IPv4 connection, you need to create an IPv4 connection.