Java IP Address Get getLocalIPList()

Here you can find the source of getLocalIPList()

Description

get Local IP List

License

Open Source License

Declaration

public static List<String> getLocalIPList() 

Method Source Code


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

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    public static List<String> getLocalIPList() {
        List<String> ipList = new ArrayList<>();
        try {/*w  ww . j a  v a 2s.  com*/
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface;
            Enumeration<InetAddress> inetAddresses;
            InetAddress inetAddress;
            String ip;
            while (networkInterfaces.hasMoreElements()) {
                networkInterface = networkInterfaces.nextElement();
                inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    inetAddress = inetAddresses.nextElement();
                    if (inetAddress instanceof Inet4Address && !"127.0.0.1".equals(inetAddress.getHostAddress())) { // IPV4
                        ip = inetAddress.getHostAddress();
                        ipList.add(ip);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("get local host error >> " + e.getMessage());
        }
        return ipList;
    }
}

Related

  1. getLocalIp(final boolean useIpv6)
  2. getLocalIpByInterfaceName(String interfaceName)
  3. getLocalIPByNetworkInterface()
  4. getLocalIPCollection()
  5. getLocalIPList()
  6. getLocalIPs()
  7. getLocalIPs()
  8. getLocalIps()
  9. getLocalIPs(boolean refresh)