Java IP Address Get getLocalIPAddresses()

Here you can find the source of getLocalIPAddresses()

Description

Gets local IPv4 addresses of the local host.

License

Apache License

Exception

Parameter Description
SocketException an exception

Return

list of IPv4 address of the local host

Declaration

public static List<InetAddress> getLocalIPAddresses() throws SocketException 

Method Source Code

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

import java.net.Inet4Address;
import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    /**/*from w  ww  .j  a  va2s  .  com*/
     * Gets local IPv4 addresses of the local host.
     *
     * @return list of IPv4 address of the local host
     * @throws SocketException
     */
    public static List<InetAddress> getLocalIPAddresses() throws SocketException {
        List<InetAddress> result = new ArrayList<>();

        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();

            // skip down, loopback, and virtual interfaces
            if ((!ni.isUp() || ni.isLoopback()) || ni.isVirtual()) {
                continue;
            }

            Enumeration<InetAddress> ips = ni.getInetAddresses();
            while (ips.hasMoreElements()) {
                InetAddress ip = ips.nextElement();
                if (ip instanceof Inet4Address) {
                    result.add((Inet4Address) ip);
                }
            }
        }
        return result;
    }
}

Related

  1. getLocalIPAddress()
  2. getLocalIPAddress()
  3. getLocalIPAddress()
  4. getLocalIpAddress()
  5. getLocalIpAddress()
  6. getLocalIpAddresses()
  7. getLocalIPAddresses()
  8. getLocalIpByInterfaceName(String interfaceName)
  9. getLocalIPByNetworkInterface()