Java IP Address Get getLocalIpAddress()

Here you can find the source of getLocalIpAddress()

Description

get Local Ip Address

License

LGPL

Declaration

public static String getLocalIpAddress() throws Exception 

Method Source Code

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

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

public class Main {
    public static String getLocalIpAddress() throws Exception {
        String ipAddress = null;/*from  w w  w . j  a  v a 2 s. c  o  m*/

        Enumeration<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces();

        while (en.hasMoreElements()) {
            NetworkInterface e = en.nextElement();
            Enumeration<InetAddress> addresses = e.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (!address.isLoopbackAddress()
                        && address.isSiteLocalAddress()) {
                    ipAddress = address.getHostName();
                    break;
                }
            }
        }

        if (ipAddress == null) {
            ipAddress = InetAddress.getLocalHost().getHostAddress();
        }

        return ipAddress;
    }
}

Related

  1. getLocalIp(final boolean useIpv6)
  2. getLocalIPAddress()
  3. getLocalIPAddress()
  4. getLocalIpAddress()
  5. getLocalIPAddress()
  6. getLocalIPAddresses()
  7. getLocalIPAddresses()
  8. getLocalIpAddresses()
  9. getLocalIpByInterfaceName(String interfaceName)