Here you can find the source of getInetAddressMap()
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.
Parameter | Description |
---|---|
SocketException | if there is an error in the underlyingI/O subsystem and/or protocol. |
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.
public static Map<InetAddress, String> getInetAddressMap() throws SocketException
//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; } }