本文整理匯總了Java中java.net.NetworkInterface.getIndex方法的典型用法代碼示例。如果您正苦於以下問題:Java NetworkInterface.getIndex方法的具體用法?Java NetworkInterface.getIndex怎麽用?Java NetworkInterface.getIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.getIndex方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getLocalAddress0
import java.net.NetworkInterface; //導入方法依賴的package包/類
private static InetAddress getLocalAddress0() {
InetAddress result = null;
try {
int lowest = Integer.MAX_VALUE;
for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics
.hasMoreElements();) {
NetworkInterface ifc = nics.nextElement();
if (ifc.isUp()) {
logger.trace("Testing interface: " + ifc.getDisplayName());
if (ifc.getIndex() < lowest || result == null) {
lowest = ifc.getIndex();
} else if (result != null) {
continue;
}
for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
InetAddress address = addrs.nextElement();
if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
logger.trace("Found non-loopback interface: " + ifc.getDisplayName());
result = address;
}
}
}
}
} catch (IOException ex) {
logger.error("Cannot get first non-loopback address", ex);
}
if (result != null && isValidAddress(result)) {
return result;
}
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
logger.warn("Unable to retrieve localhost");
}
return null;
}