diff options
author | Elliott Hughes <enh@google.com> | 2010-03-23 14:21:27 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-03-23 14:21:27 -0700 |
commit | ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2 (patch) | |
tree | 518ec92ca325a160f83bc1063307c506705bec04 | |
parent | 06d6203f27c70d6f83f05e393559eff91d72ecc6 (diff) | |
download | libcore-ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2.zip libcore-ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2.tar.gz libcore-ce64852f32d7d10ebd61a9c8f9ba2e91068f9bd2.tar.bz2 |
Improve java.net.InetAddress.getLocalHost documentation.
Bug: 1518707
Change-Id: I741a7a28325320949e84e997e6a49d3356c9a308
-rw-r--r-- | luni/src/main/java/java/net/InetAddress.java | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/luni/src/main/java/java/net/InetAddress.java b/luni/src/main/java/java/net/InetAddress.java index ad96ed6..41d49b2 100644 --- a/luni/src/main/java/java/net/InetAddress.java +++ b/luni/src/main/java/java/net/InetAddress.java @@ -410,11 +410,39 @@ public class InetAddress implements Serializable { } /** - * Gets the local host address if the security policy allows this. - * Otherwise, gets the loopback address which allows this machine to be - * contacted. + * Returns an {@code InetAddress} for the local host if possible, or the + * loopback address otherwise. This method works by getting the hostname, + * performing a DNS lookup, and then taking the first returned address. + * For devices with multiple network interfaces and/or multiple addresses + * per interface, this does not necessarily return the {@code InetAddress} + * you want. * - * @return the {@code InetAddress} representing the local host. + * <p>Multiple interface/address configurations were relatively rare + * when this API was designed, but multiple interfaces are the default for + * modern mobile devices (with separate wifi and radio interfaces), and + * the need to support both IPv4 and IPv6 has made multiple addresses + * commonplace. New code should thus avoid this method except where it's + * basically being used to get a loopback address or equivalent. + * + * <p>There are two main ways to get a more specific answer: + * <ul> + * <li>If you have a connected socket, you should probably use + * {@link Socket#getLocalAddress} instead: that will give you the address + * that's actually in use for that connection. (It's not possible to ask + * the question "what local address would a connection to a given remote + * address use?"; you have to actually make the connection and see.)</li> + * <li>For other use cases, see {@link NetworkInterface}, which lets you + * enumerate all available network interfaces and their addresses.</li> + * </ul> + * + * <p>Note that if the host doesn't have a hostname set – as + * Android devices typically don't – this method will + * effectively return the loopback address, albeit by getting the name + * {@code localhost} and then doing a lookup to translate that to + * {@code 127.0.0.1}. + * + * @return an {@code InetAddress} representing the local host, or the + * loopback address. * @throws UnknownHostException * if the address lookup fails. */ |