Java IP Address Get getLocalIp()

Here you can find the source of getLocalIp()

Description

get Local Ip

License

Open Source License

Declaration

public static String getLocalIp() 

Method Source Code

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

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Main {
    public static String getLocalIp() {
        try {/*w  w w .j a  v a2  s  .  c om*/
            Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
            while (ifs.hasMoreElements()) {
                NetworkInterface iface = ifs.nextElement();
                if (iface.isUp()) {
                    Enumeration<InetAddress> adds = iface.getInetAddresses();
                    while (adds.hasMoreElements()) {
                        InetAddress addr = adds.nextElement();
                        if (!isIpV6(addr) && !addr.isAnyLocalAddress() && !addr.isLoopbackAddress()) {
                            return addr.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException se) {
            se.printStackTrace();
        }
        return "NOIP";
    }

    private static boolean isIpV6(InetAddress addr) {
        return addr.getHostAddress().contains(":");
    }
}

Related

  1. getLocalIP()
  2. getLocalIP()
  3. getLocalIP()
  4. getLocalIp()
  5. getLocalIP()
  6. getLocalIp()
  7. getLocalIp()
  8. getLocalIP()
  9. getLocalIP()