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;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class Main {

    public static String getLocalIp() {

        try {/*from  w  w  w  .  j av a2  s. c  o m*/
            for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {

                NetworkInterface item = e.nextElement();

                for (InterfaceAddress address : item.getInterfaceAddresses()) {
                    if (address.getAddress() instanceof Inet4Address) {
                        Inet4Address inet4Address = (Inet4Address) address.getAddress();
                        if (inet4Address.isLoopbackAddress()) {
                            continue;
                        }
                        System.out.println(inet4Address.getHostAddress());
                        return inet4Address.getHostAddress();
                    }
                }

            }
        } catch (Exception e) {
            throw new IllegalStateException("no ip");
        }
        throw new IllegalStateException("no ip");
    }
}

Related

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