Java Local Address Get getLocalAddresses()

Here you can find the source of getLocalAddresses()

Description

Get a set of local host addresses.

License

Open Source License

Exception

Parameter Description
SocketException if there was a problem talking to the network.

Return

Set of local host addresses.

Declaration

public static Set<InetAddress> getLocalAddresses() throws SocketException 

Method Source Code


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

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**/*w  w  w.j  a va  2 s  .c o  m*/
     * Get a set of local host addresses.
     *
     * @return Set of local host addresses.
     * @throws SocketException if there was a problem talking to the network.
     */
    public static Set<InetAddress> getLocalAddresses() throws SocketException {

        Set<InetAddress> result = new HashSet<InetAddress>();

        for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {

            NetworkInterface ni = e.nextElement();

            // complain(LOG_ALERT, getLogChannel(), "Interface: " +
            // ni.getDisplayName());

            for (Enumeration<InetAddress> e2 = ni.getInetAddresses(); e2.hasMoreElements();) {

                InetAddress niAddress = e2.nextElement();

                // complain(LOG_ALERT, getLogChannel(), "Address: " +
                // niAddress);

                result.add(niAddress);
            }
        }

        return result;
    }
}

Related

  1. getLocalAddress(String filter)
  2. getLocalAddress0()
  3. getLocalAddressAsString()
  4. getLocalAddresses()
  5. getLocalAddresses()
  6. getLocalAddresses()
  7. getLocalAddressFromNetworkInterfacesListeningOnPort(int pPort)
  8. getLocalAddressStrings()
  9. getLocalAddressWithMulticast()