Java.Net.InetAddress.LocalHost Property
Returns an InetAddress for the local host if possible, or the loopback address otherwise.

Syntax

[get: Android.Runtime.Register("getLocalHost", "()Ljava/net/InetAddress;", "GetGetLocalHostHandler")]
public static InetAddress LocalHost { get; }

Value

Documentation for this section has not yet been entered.

Exceptions

TypeReason
Java.Net.UnknownHostExceptionif the address lookup fails.

Remarks

Returns an 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 InetAddress you want.

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.

There are two main ways to get a more specific answer:

  • If you have a connected socket, you should probably use Socket.LocalAddress 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.)
  • For other use cases, see Java.Net.NetworkInterface, which lets you enumerate all available network interfaces and their addresses.

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 localhost and then doing a lookup to translate that to 127.0.0.1.

[Android Documentation]

Requirements

Namespace: Java.Net
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1