Java IP Address Get getLocalhostIp()

Here you can find the source of getLocalhostIp()

Description

get Localhost Ip

License

Open Source License

Declaration

public static String getLocalhostIp() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.*;
import java.util.Enumeration;

public class Main {
    public static String getLocalhostIp() {
        try {//w  ww .ja v a2 s  . co m
            for (final Enumeration<NetworkInterface> interfaces = NetworkInterface
                    .getNetworkInterfaces(); interfaces.hasMoreElements();) {
                final NetworkInterface cur = interfaces.nextElement();
                if (cur.isLoopback()) {
                    continue;
                }

                if (cur.getName().startsWith("vnic")) {
                    // skip parallels virtual interfaces
                    continue;
                }

                for (final InterfaceAddress interfaceAddress : cur.getInterfaceAddresses()) {
                    final InetAddress inetAddress = interfaceAddress.getAddress();
                    if (!((inetAddress instanceof Inet4Address))) {
                        continue;
                    }

                    return inetAddress.getHostAddress();
                }
            }

            return "localhost";
        } catch (Throwable e) {
            return "localhost";
        }
    }
}

Related

  1. getLanIPAddress()
  2. getLinuxIPAddress()
  3. getLocalAddresses(boolean ipv4only)
  4. getLocalHostIP()
  5. getLocalHostIp()
  6. getLocalHostIp()
  7. getLocalHostIPAdress()
  8. getLocalHostIps()
  9. getLocalHostIps()