Java IP Address Get getIPAddresses()

Here you can find the source of getIPAddresses()

Description

List all IP addresses of the device, except loopback addresses.

License

Apache License

Exception

Parameter Description
IOException an exception

Return

a List of IP addresses

Declaration

public static List<InetAddress> getIPAddresses() throws IOException 

Method Source Code


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

import java.io.IOException;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    /**//from   ww  w  .j  a  v  a2 s  .  c  o  m
     * List all IP addresses of the device, except loopback addresses.
     * 
     * @return a List of IP addresses
     * @throws IOException
     */
    public static List<InetAddress> getIPAddresses() throws IOException {
        List<InetAddress> addresses = new ArrayList<InetAddress>();
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress a : addrs) {
                if (a.isLoopbackAddress()) {
                    continue;
                }
                addresses.add(a);
            }
        }
        return addresses;
    }
}

Related

  1. getIPAddress(String hostname)
  2. getIpAddress(String server)
  3. getIPAddresses()
  4. getIpAddresses()
  5. getIpAddresses()
  6. getIpAddressesFromNic()
  7. getIpAddressExternal()
  8. getIPAddressFromBytes(byte[] bytes)
  9. getIPAddressFromNetworkInterface(String ifaceName)