Java IP Address Get getLocalIP()

Here you can find the source of getLocalIP()

Description

get Local IP

License

Apache License

Declaration

public static String getLocalIP() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.InetAddress;
import java.net.NetworkInterface;

import java.util.*;

public class Main {

    public static String getLocalIP() {
        String sIP = "";
        InetAddress ip = null;// w w w.j  a  v a 2  s  . c om
        try {
            boolean bFindIP = false;
            Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
                    .getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                if (bFindIP) {
                    break;
                }
                NetworkInterface ni = (NetworkInterface) netInterfaces
                        .nextElement();
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = (InetAddress) ips.nextElement();
                    if (!ip.isLoopbackAddress()
                            && ip.getHostAddress().matches(
                                    "(\\d{1,3}\\.){3}\\d{1,3}")) {
                        bFindIP = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != ip) {
            sIP = ip.getHostAddress();
        }
        return sIP;
    }
}

Related

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