Java IP Address Get getLocalIPList()

Here you can find the source of getLocalIPList()

Description

get Local IP List

License

LGPL

Declaration

public static List<InetAddress> getLocalIPList() throws SocketException, UnknownHostException 

Method Source Code

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

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

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

public class Main {
    public static List<InetAddress> getLocalIPList() throws SocketException, UnknownHostException {
        List<InetAddress> result = new ArrayList<InetAddress>();

        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface net : Collections.list(nets)) {
            if (!net.isLoopback() && !net.isVirtual() && !net.isPointToPoint() && net.isUp()) {
                for (InetAddress curAddress : Collections.list(net.getInetAddresses())) {

                    if (curAddress != null && !curAddress.isMulticastAddress()
                            && curAddress instanceof Inet4Address) {
                        result.add(curAddress);
                    }/* ww  w . j av a  2s  .c  o  m*/
                }
            }
        }

        // Add the loopback address at the end
        result.add(InetAddress.getLocalHost());

        return result;
    }
}

Related

  1. getLocalIPAddresses()
  2. getLocalIpAddresses()
  3. getLocalIpByInterfaceName(String interfaceName)
  4. getLocalIPByNetworkInterface()
  5. getLocalIPCollection()
  6. getLocalIps()
  7. getLocalIPs()
  8. getLocalIPs()
  9. getLocalIPs(boolean refresh)