suggest improvement for java doc for getting address of local host

Peter Firmstone peter.firmstone at zeus.net.au
Fri Aug 5 07:41:57 UTC 2022


On 5/08/2022 4:44 pm, Alan Bateman wrote:

> On 04/08/2022 23:24, mark.yagnatinsky at barclays.com wrote:
>>
>> I suspect that this is the wrong list; please redirect me if so.
>>
>> The docs for this method:
>>
>> https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/net/InetAddress.html#getLocalHost()
>>
>> say that it “Returns the address of the local host. This is achieved 
>> by retrieving the name of the host from the system, then resolving 
>> that name into an InetAddress.”
>>
>> This is true enough in simple cases, but horribly misleading in 
>> non-trivial cases.
>>
>> As far as I can tell it can’t return “the address” because the local 
>> host may have multiple addresses.
>>
>> Instead it returns “an address” of the local host.  My laptop right 
>> now has 3 addresses: IPv6 loopback, IPv4 from home WiFi, and IPv4 
>> from company network via VPN.
>>
>> As far as I can tell (please correct me if I’m wrong!!) if there are 
>> multiple addresses, the current implementation makes no effort to 
>> pick a more “useful” one.
>>
>> Instead, it takes whatever addresses it gets from the OS and returns 
>> the first one.
>>
>>
> You need to click through to the Networking Properties to read more 
> about configuration. The long standing default has been to prefer IPv4 
> over IPv6 addresses. The OpenJDK net-dev mailing list is the place to 
> follow-up on this.
>
> -Alan

Set the following property to prefer IPv6:

java.net.preferIPv6Addresses=TRUE

In our software we also use this property to determine whether to prefer 
IPv4 or IPv6, eg multicast. 
https://github.com/pfirmstone/JGDMS/blob/trunk/JGDMS/jgdms-discovery-providers/src/main/java/org/apache/river/discovery/x500/sha512withrsa/package.html

It's always good to utilise existing properties when possible.

-- 
Regards,
  
Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20220805/b11506e8/attachment.htm>


More information about the jdk-dev mailing list