當前位置: 首頁>>代碼示例>>Java>>正文


Java NetworkInterface.getByIndex方法代碼示例

本文整理匯總了Java中java.net.NetworkInterface.getByIndex方法的典型用法代碼示例。如果您正苦於以下問題:Java NetworkInterface.getByIndex方法的具體用法?Java NetworkInterface.getByIndex怎麽用?Java NetworkInterface.getByIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.NetworkInterface的用法示例。


在下文中一共展示了NetworkInterface.getByIndex方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getMacAddress

import java.net.NetworkInterface; //導入方法依賴的package包/類
public static byte[] getMacAddress() {
	String networkInterfaceConfig = getPropertyValue("network.interface").toString();
	NetworkInterface nif = null;
	byte[] macAddress = null;
	
	try {
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
		} else {
			nif = NetworkInterface.getByName(networkInterfaceConfig);
		}
		macAddress = nif.getHardwareAddress();
	} catch (SocketException e) {
		getLogger().error("Failed to retrieve local mac address (SocketException)", e);
	}
	
	return macAddress;
}
 
開發者ID:V2GClarity,項目名稱:RISE-V2G,代碼行數:19,代碼來源:MiscUtils.java

示例2: getInterfaceIp

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private static Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return OspfUtil.DEFAULTIP;
    }
    return ipAddress;
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:25,代碼來源:OspfConfigUtil.java

示例3: getInterfaceIp

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return OspfUtil.DEFAULTIP;
    }

    return ipAddress;
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:26,代碼來源:Controller.java

示例4: getInterfaceIp

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return IsisConstants.DEFAULTIP;
    }
    return ipAddress;
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:25,代碼來源:Controller.java

示例5: getLinkLocalAddress

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * Determines the link-local IPv6 address which is configured on the network interface provided
 * in the properties file.
 * @return The link-local address given as a String
 */
public static Inet6Address getLinkLocalAddress() {
	String networkInterfaceConfig = getPropertyValue("network.interface").toString();
	
	NetworkInterface nif = null;
	
	try {
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
		} else {
			nif = NetworkInterface.getByName(networkInterfaceConfig);
		}
		Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
		
		while (inetAddresses.hasMoreElements()) {
			InetAddress inetAddress = inetAddresses.nextElement();
			
			if (inetAddress.getClass() == Inet6Address.class && inetAddress.isLinkLocalAddress()) {
				return (Inet6Address) inetAddress;
			}
		}
		
		getLogger().fatal("No IPv6 link-local address found on the network interface '" +
				nif.getDisplayName() + "' configured in the properties file");
	} catch (SocketException e) {
		getLogger().fatal("SocketException while trying to get network interface for configured name " +
						  networkInterfaceConfig + "'", e); 
	} catch (NullPointerException | NumberFormatException e2) {
		getLogger().fatal("No network interface for configured network interface index '" + 
						  networkInterfaceConfig + "' found");
	}
	
	return null;
}
 
開發者ID:V2GClarity,項目名稱:RISE-V2G,代碼行數:39,代碼來源:MiscUtils.java

示例6: getInterfaceMac

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * Returns interface MAC by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private MacAddress getInterfaceMac(int interfaceIndex) {
    MacAddress macAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        macAddress = MacAddress.valueOf(networkInterface.getHardwareAddress());
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return macAddress;
    }

    return macAddress;
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:19,代碼來源:Controller.java

示例7: getHWSeq

import java.net.NetworkInterface; //導入方法依賴的package包/類
static private long getHWSeq()
{
    if (hwseq != 0)
        return hwseq;

    for (int ii = 0; ii < 50; ii++)
    {
        try
        {
            NetworkInterface ni = NetworkInterface.getByIndex(ii);
            if (ni != null) // real interface
            {
                byte[] hwaddr = ni.getHardwareAddress();
                if (hwaddr == null) continue;

                String dname  = ni.getDisplayName();
                if (dname.startsWith("Microsoft"))  continue;
                if (dname.startsWith("VMware"))     continue;
                if (dname.startsWith("VirtualBox")) continue;
                if (dname.contains("Tunneling"))    continue;

                // HW will be somewhat unique, we use the above to skip things that generally are not
                hwseq = (VARIANT1 & VARIANT_MASK)| (new Random().nextLong() & CLKSEQ_MASK) | (new BigInteger(1, hwaddr).longValue() & NODE_MASK);
                break;
            }
        }
        catch (SocketException se) {}
    }

    return hwseq;
}
 
開發者ID:drytoastman,項目名稱:scorekeeperfrontend,代碼行數:32,代碼來源:IdGenerator.java

示例8: findIPv6Interface

import java.net.NetworkInterface; //導入方法依賴的package包/類
private NetworkInterface findIPv6Interface(Inet6Address address) throws IOException {
    if(blacklisted.isEmpty()) {
        if(log.isDebugEnabled()) {
            log.debug("Ignore IP6 default network interface setup with empty blacklist");
        }
        return null;
    }
    if(address.getScopeId() != 0) {
        if(log.isDebugEnabled()) {
            log.debug(String.format("Ignore IP6 default network interface setup for address with scope identifier %d", address.getScopeId()));
        }
        return null;
    }
    // If we find an interface name en0 that supports IPv6 make it the default.
    // We must use the index of the network interface. Referencing the interface by name will still
    // set the scope id to '0' referencing the awdl0 interface that is first in the list of enumerated
    // network interfaces instead of its correct index in <code>java.net.Inet6Address</code>
    // Use private API to defer the numeric format for the address
    List<Integer> indexes = new ArrayList<Integer>();
    final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    while(enumeration.hasMoreElements()) {
        indexes.add(enumeration.nextElement().getIndex());
    }
    for(Integer index : indexes) {
        final NetworkInterface n = NetworkInterface.getByIndex(index);
        if(log.isDebugEnabled()) {
            log.debug(String.format("Evaluate interface with %s index %d", n, index));
        }
        if(!n.isUp()) {
            if(log.isDebugEnabled()) {
                log.debug(String.format("Ignore interface %s not up", n));
            }
            continue;
        }
        if(blacklisted.contains(n.getName())) {
            log.warn(String.format("Ignore network interface %s disabled with blacklist", n));
            continue;
        }
        for(InterfaceAddress i : n.getInterfaceAddresses()) {
            if(i.getAddress() instanceof Inet6Address) {
                if(log.isInfoEnabled()) {
                    log.info(String.format("Selected network interface %s", n));
                }
                return n;
            }
        }
        log.warn(String.format("No IPv6 for interface %s", n));
    }
    log.warn("No network interface found for IPv6");
    return null;
}
 
開發者ID:iterate-ch,項目名稱:cyberduck,代碼行數:52,代碼來源:NetworkInterfaceAwareSocketFactory.java


注:本文中的java.net.NetworkInterface.getByIndex方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。