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() throws Exception 

Method Source Code

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

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

import java.util.Enumeration;

public class Main {
    public static String getLocalIP() throws Exception {
        String localip = "";
        InetAddress inetaddress = InetAddress.getLocalHost();
        localip = inetaddress.getHostAddress();
        if ("127.0.0.1".equals(localip) || "localhost".equals(localip)
                || localip.indexOf("local") != -1) {
            Enumeration<NetworkInterface> allnetinterface = NetworkInterface
                    .getNetworkInterfaces();
            while (allnetinterface.hasMoreElements()) {
                NetworkInterface net = allnetinterface.nextElement();
                Enumeration<InetAddress> ipenum = net.getInetAddresses();
                while (ipenum.hasMoreElements()) {
                    InetAddress ip = ipenum.nextElement();
                    String t_ip = ip.getHostAddress();
                    if (!"127.0.0.1".equals(t_ip)
                            && !"localhost".equals(t_ip)
                            && !t_ip.contains(":")) {
                        localip = t_ip;//from www .j ava2  s. c  o  m
                    }
                }
            }
        }
        return localip;
    }
}

Related

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