Java InetAddress Create getInetAddresses()

Here you can find the source of getInetAddresses()

Description

Get all Internet addresses of this computer.

License

Open Source License

Exception

Parameter Description
SocketException an exception

Return

all Internet addresses of this computer.

Declaration

public static Set<InetAddress> getInetAddresses() throws SocketException 

Method Source Code

//package com.java2s;
/* Copyright (c) 1996-2015, OPC Foundation. All rights reserved.
   The source code in this file is covered under a dual-license scenario:
 - RCL: for OPC Foundation members in good-standing
 - GPL V2: everybody else/*from www  .j  a v a  2s  .c  om*/
   RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
   GNU General Public License as published by the Free Software Foundation;
   version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
   This source code is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

import java.net.Inet6Address;
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 {
    /**
     * Get all Internet addresses of this computer. Excludes IPv6 addresses.
     * 
     * @return all Internet addresses of this computer.
     * @throws SocketException
     */
    public static Set<InetAddress> getInetAddresses() throws SocketException {
        return getInetAddresses(false);
    }

    /**
     * Get all Internet addresses of this computer.
     * 
     * @param enableIPv6 Set true to enable IPv6 addressing. Requires Java 7 or later on Windows platforms.
     * 
     * @return all Internet addresses of this computer
     * @throws SocketException
     */
    public static Set<InetAddress> getInetAddresses(boolean enableIPv6) throws SocketException {
        Set<InetAddress> result = new HashSet<InetAddress>();
        Enumeration<NetworkInterface> nids = NetworkInterface.getNetworkInterfaces();
        for (; nids.hasMoreElements();) {
            Enumeration<InetAddress> addrs = nids.nextElement().getInetAddresses();
            for (; addrs.hasMoreElements();) {
                InetAddress addr = addrs.nextElement();
                if (!(addr instanceof Inet6Address) || enableIPv6)
                    result.add(addr);
            }
        }
        return result;
    }
}

Related

  1. getInetAddress(String host)
  2. getInetAddress(String hostName)
  3. getInetAddress(String name)
  4. getInetAddress(String str)
  5. getInetAddressByName(String name)
  6. getInetAddresses(String filter)
  7. getInetAddressFor(final Inet6Address inetAddress, String scope)
  8. getInetAddressFromAsciiIP(String ip)
  9. getInetAddressFromConfigString( String fixedDeviceConfigSring)