Java InetAddress Create getInetAddressMap()

Here you can find the source of getInetAddressMap()

Description

Method that returns a Map in which the key component of each element is one of the addresses of one of the network interface cards (nics) installed on the current node, and the corresponding value component is the name of the associated nic to which that address is assigned.

License

Open Source License

Exception

Parameter Description
SocketException if there is an error in the underlyingI/O subsystem and/or protocol.

Return

a Map of key-value pairs in which the key is an instance of InetAddress referencing the address of one of the network interface cards installed on the current node, and the corresponding value is a String referencing the name of the associated network interface to which the address is assigned.

Declaration

public static Map<InetAddress, String> getInetAddressMap()
        throws SocketException 

Method Source Code

//package com.java2s;
/*//  ww  w.  ja v  a2  s .c  o  m

 Copyright (C) SYSTAP, LLC 2006-2008.  All rights reserved.

 Contact:
 SYSTAP, LLC
 4501 Tower Road
 Greensboro, NC 27410
 licenses@bigdata.com

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program 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.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

import java.net.InetAddress;

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

import java.util.Enumeration;
import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Method that returns a <code>Map</code> in which the key component
     * of each element is one of the addresses of one of the network
     * interface cards (nics) installed on the current node, and the
     * corresponding value component is the name of the associated
     * nic to which that address is assigned.
     *
     * @return a <code>Map</code> of key-value pairs in which the key
     *         is an instance of <code>InetAddress</code> referencing
     *         the address of one of the network interface cards installed
     *         on the current node, and the corresponding value is a
     *         <code>String</code> referencing the name of the associated
     *         network interface to which the address is assigned.
     *
     * @throws SocketException if there is an error in the underlying
     *         I/O subsystem and/or protocol.
     */
    public static Map<InetAddress, String> getInetAddressMap()
            throws SocketException {
        Map<InetAddress, String> retMap = new HashMap<InetAddress, String>();

        //get all nics on the current node
        Enumeration<NetworkInterface> nics = NetworkInterface
                .getNetworkInterfaces();

        while (nics.hasMoreElements()) {
            NetworkInterface curNic = nics.nextElement();
            Enumeration<InetAddress> curNicAddrs = curNic
                    .getInetAddresses();
            String curNicName = curNic.getName();
            while (curNicAddrs.hasMoreElements()) {
                retMap.put(curNicAddrs.nextElement(), curNicName);
            }
        }
        return retMap;
    }
}

Related

  1. getInetAddresses(String filter)
  2. getInetAddressFor(final Inet6Address inetAddress, String scope)
  3. getInetAddressFromAsciiIP(String ip)
  4. getInetAddressFromConfigString( String fixedDeviceConfigSring)
  5. getInetAddressFromString(String s)
  6. getInetAddressMap()
  7. getInt32FromAddress(Inet4Address inetAddress)
  8. getInterface(InetAddress addr)
  9. getIP(InetAddress ip)