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.net.SocketException;

import java.util.*;

public class Main {

    public static String getLocalIP() {
        StringBuilder IFCONFIG = new StringBuilder();
        try {// w ww . j  a  v a  2 s .  com
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
                            && inetAddress.isSiteLocalAddress()) {
                        IFCONFIG.append(inetAddress.getHostAddress().toString());
                    }

                }
            }
        } catch (SocketException ex) {
        }

        return IFCONFIG.toString();
    }
}

Related

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