本文整理匯總了Java中java.net.InetAddress.getClass方法的典型用法代碼示例。如果您正苦於以下問題:Java InetAddress.getClass方法的具體用法?Java InetAddress.getClass怎麽用?Java InetAddress.getClass使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.getClass方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getInventoryEndpoints
import java.net.InetAddress; //導入方法依賴的package包/類
public List<String> getInventoryEndpoints() {
List<String> endpoints = new ArrayList<>(2);
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress addr : Collections.list(inetAddresses)) {
if (!addr.isLinkLocalAddress() && addr.getClass() == Inet4Address.class) {
endpoints.add(
String.format("http://%s:%d/v1/grapher/inventory", addr.getHostAddress(), port));
}
}
}
} catch (SocketException e) {
logger.error("Exception looking up network interfaces", e);
}
return endpoints;
}
示例2: getLinkLocalAddress
import java.net.InetAddress; //導入方法依賴的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;
}