Java Utililty Methods Local Address Get

List of utility methods to do Local Address Get

Description

The list of methods to do Local Address Get are organized into topic(s).

Method

StringgetLocalAddr()
get Local Addr
Enumeration<NetworkInterface> interfaces = null;
try {
    interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface ifc = interfaces.nextElement();
        if (ifc.isLoopback() || ifc.isVirtual() || !ifc.isUp()) {
            continue;
        Enumeration<InetAddress> addressesOfAnInterface = ifc.getInetAddresses();
        while (addressesOfAnInterface.hasMoreElements()) {
            InetAddress address = addressesOfAnInterface.nextElement();
            if (address.isSiteLocalAddress()) {
                return address.getHostAddress();
} catch (SocketException e) {
    e.printStackTrace();
    return null;
return null;
InetAddressgetLocalAddr()
Will try to find the local IP V4 address there is not the loopback address
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface dev = interfaces.nextElement();
    String devName = dev.getDisplayName();
    Enumeration<InetAddress> addresses = dev.getInetAddresses();
    while (addresses.hasMoreElements()) {
        InetAddress addr = addresses.nextElement();
        if (addr instanceof Inet6Address)
...
StringgetLocalAddress()
This method returns the local IP address as a String.
try {
    return InetAddress.getLocalHost().getHostAddress();
} catch (Exception ex) {
    return "0.0.0.0";
InetAddressgetLocalAddress()
get Local Address
InetAddress addr = null;
try {
    Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
    InetAddress ip = null;
    while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
        ip = (InetAddress) ni.getInetAddresses().nextElement();
        if (!ip.isLoopbackAddress()) {
...
StringgetLocalAddress()
Gets the local address.
try {
    InetAddress localMachine = InetAddress.getLocalHost();
    return localMachine.getHostAddress();
} catch (UnknownHostException uhe) {
    return "";
InetAddressgetLocalAddress()
The preferred local address.
NetworkInterface result_intf = null, intf;
InetAddress result_addr = null, addr;
Enumeration<InetAddress> eips = null;
Enumeration<NetworkInterface> eintfs = null;
try {
    eintfs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e1) {
    e1.printStackTrace();
...
InetAddressgetLocalAddress()
get Local Address
try {
    Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    ArrayList<Inet4Address> ipv4Result = new ArrayList<Inet4Address>();
    ArrayList<Inet6Address> ipv6Result = new ArrayList<Inet6Address>();
    while (enumeration.hasMoreElements()) {
        final NetworkInterface networkInterface = enumeration.nextElement();
        final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
        while (en.hasMoreElements()) {
...
InetAddressgetLocalAddress()
get Local Address
InetAddress addr = InetAddress.getLocalHost();
if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
    return addr;
try {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    if (interfaces != null) {
        while (interfaces.hasMoreElements()) {
...
StringgetLocalAddress()
get Local Address
return InetAddress.getLocalHost().getHostAddress();
InetAddressgetLocalAddress()
get Local Address
if (LOCAL_ADDRESS != null)
    return LOCAL_ADDRESS;
InetAddress localAddress = getLocalAddress0();
LOCAL_ADDRESS = localAddress;
return localAddress;