本文整理汇总了Java中java.net.InetAddress.isAnyLocalAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.isAnyLocalAddress方法的具体用法?Java InetAddress.isAnyLocalAddress怎么用?Java InetAddress.isAnyLocalAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.isAnyLocalAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sortKey
import java.net.InetAddress; //导入方法依赖的package包/类
/** Sorts an address by preference. This way code like publishing can just pick the first one */
static int sortKey(InetAddress address, boolean prefer_v6) {
int key = address.getAddress().length;
if (prefer_v6) {
key = -key;
}
if (address.isAnyLocalAddress()) {
key += 5;
}
if (address.isMulticastAddress()) {
key += 4;
}
if (address.isLoopbackAddress()) {
key += 3;
}
if (address.isLinkLocalAddress()) {
key += 2;
}
if (address.isSiteLocalAddress()) {
key += 1;
}
return key;
}
示例2: isLocalAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Given an InetAddress, checks to see if the address is a local address, by comparing the address
* with all the interfaces on the node.
* @param addr address to check if it is local node's address
* @return true if the address corresponds to the local node
*/
public static boolean isLocalAddress(InetAddress addr) {
// Check if the address is any local or loop back
boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
// Check if the address is defined on any interface
if (!local) {
try {
local = NetworkInterface.getByInetAddress(addr) != null;
} catch (SocketException e) {
local = false;
}
}
return local;
}
示例3: getLocalInetAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Retrieve the first validated local ip address(the Public and LAN ip addresses are validated).
*
* @return the local address
* @throws SocketException the socket exception
*/
public static InetAddress getLocalInetAddress() throws SocketException {
// enumerates all network interfaces
Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();
while (enu.hasMoreElements()) {
NetworkInterface ni = enu.nextElement();
if (ni.isLoopback()) {
continue;
}
Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
while (addressEnumeration.hasMoreElements()) {
InetAddress address = addressEnumeration.nextElement();
// ignores all invalidated addresses
if (address.isLinkLocalAddress() || address.isLoopbackAddress() || address.isAnyLocalAddress()) {
continue;
}
return address;
}
}
throw new RuntimeException("No validated local address!");
}
示例4: normalizeHostAndPort
import java.net.InetAddress; //导入方法依赖的package包/类
static String normalizeHostAndPort(String address) {
try {
if (address == null) {
return null;
}
URI uri = new URI("http://" + address.trim());
String host = uri.getHost();
if (host == null || host.equals("") || host.equals("localhost") ||
host.equals("127.0.0.1") || host.equals("[0:0:0:0:0:0:0:1]")) {
return null;
}
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress() ||
inetAddress.isLinkLocalAddress()) {
return null;
}
int port = uri.getPort();
return port == -1 ? host : host + ':' + port;
} catch (URISyntaxException |UnknownHostException e) {
return null;
}
}
示例5: substituteForWildcardAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Substitute a default host in the case that an address has been configured
* with a wildcard. This is used, for example, when determining the HTTP
* address of the NN -- if it's configured to bind to 0.0.0.0, we want to
* substitute the hostname from the filesystem URI rather than trying to
* connect to 0.0.0.0.
* @param configuredAddress the address found in the configuration
* @param defaultHost the host to substitute with, if configuredAddress
* is a local/wildcard address.
* @return the substituted address
* @throws IOException if it is a wildcard address and security is enabled
*/
@VisibleForTesting
static String substituteForWildcardAddress(String configuredAddress,
String defaultHost) throws IOException {
InetSocketAddress sockAddr = NetUtils.createSocketAddr(configuredAddress);
InetSocketAddress defaultSockAddr = NetUtils.createSocketAddr(defaultHost
+ ":0");
final InetAddress addr = sockAddr.getAddress();
if (addr != null && addr.isAnyLocalAddress()) {
if (UserGroupInformation.isSecurityEnabled() &&
defaultSockAddr.getAddress().isAnyLocalAddress()) {
throw new IOException("Cannot use a wildcard address with security. " +
"Must explicitly set bind address for Kerberos");
}
return defaultHost + ":" + sockAddr.getPort();
} else {
return configuredAddress;
}
}
示例6: getResolvedAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private static String getResolvedAddress(InetSocketAddress address) {
address = NetUtils.getConnectAddress(address);
StringBuilder sb = new StringBuilder();
InetAddress resolved = address.getAddress();
if (resolved == null || resolved.isAnyLocalAddress() ||
resolved.isLoopbackAddress()) {
String lh = address.getHostName();
try {
lh = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
//Ignore and fallback.
}
sb.append(lh);
} else {
sb.append(address.getHostName());
}
sb.append(":").append(address.getPort());
return sb.toString();
}
示例7: getResolvedMRHistoryWebAppURLWithoutScheme
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getResolvedMRHistoryWebAppURLWithoutScheme(
Configuration conf, boolean isSSLEnabled) {
InetSocketAddress address = null;
if (isSSLEnabled) {
address =
conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_PORT);
} else {
address =
conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS,
JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS,
JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_PORT); }
address = NetUtils.getConnectAddress(address);
StringBuffer sb = new StringBuffer();
InetAddress resolved = address.getAddress();
if (resolved == null || resolved.isAnyLocalAddress() ||
resolved.isLoopbackAddress()) {
String lh = address.getHostName();
try {
lh = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
//Ignore and fallback.
}
sb.append(lh);
} else {
sb.append(address.getHostName());
}
sb.append(":").append(address.getPort());
return sb.toString();
}
示例8: isGoodV4Address
import java.net.InetAddress; //导入方法依赖的package包/类
private static boolean isGoodV4Address(InetAddress address)
{
return address instanceof Inet4Address &&
!address.isAnyLocalAddress() &&
!address.isLoopbackAddress() &&
!address.isMulticastAddress();
}
示例9: HTTPSession
import java.net.InetAddress; //导入方法依赖的package包/类
public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) {
this.tempFileManager = tempFileManager;
this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE);
this.outputStream = outputStream;
this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString();
this.remoteHostname = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "localhost" : inetAddress.getHostName().toString();
this.headers = new HashMap<String, String>();
}
示例10: initAddr
import java.net.InetAddress; //导入方法依赖的package包/类
private void initAddr() throws UnknownHostException {
InetAddress host = InetAddress.getByName(config.getHost());
if (!host.isAnyLocalAddress()) {
throw new RuntimeException("<host> is not local");
}
udpHost = host.getHostAddress();
udpPort = config.getPort();
udpAddr = new InetSocketAddress(udpHost, udpPort);
}
示例11: isLocalAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Given an InetAddress, checks to see if the address is a local address, by
* comparing the address with all the interfaces on the node.
* @param addr address to check if it is local node's address
* @return true if the address corresponds to the local node
*/
public static boolean isLocalAddress(InetAddress addr) {
// Check if the address is any local or loop back
boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
// Check if the address is defined on any interface
if (!local) {
try {
local = NetworkInterface.getByInetAddress(addr) != null;
} catch (SocketException e) {
local = false;
}
}
return local;
}
示例12: isValidLocalAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Tests whether a given address is a valid local address.
*
* @param address an address
* @return true if the address is a valid local address
*/
public static boolean isValidLocalAddress(final InetAddress address) {
boolean local = address.isAnyLocalAddress() || address.isLoopbackAddress();
if (!local) {
try {
local = NetworkInterface.getByInetAddress(address) != null;
} catch (final SocketException e) {
local = false;
}
}
return local;
}
示例13: setHost
import java.net.InetAddress; //导入方法依赖的package包/类
public void setHost(String host)
{
try
{
InetAddress hostAddress = InetAddress.getByName(host);
if (!hostAddress.isAnyLocalAddress())
{
this.host = hostAddress;
}
}
catch (UnknownHostException e)
{
throw new RuntimeException(e.toString());
}
}
示例14: IpType
import java.net.InetAddress; //导入方法依赖的package包/类
public static String IpType(InetAddress inetAddress)
{
try
{
final String ipVersion;
if (inetAddress instanceof Inet4Address)
ipVersion = "ipv4";
else if (inetAddress instanceof Inet6Address)
ipVersion = "ipv6";
else
ipVersion = "ipv?";
if (inetAddress.isAnyLocalAddress())
return "wildcard_" + ipVersion;
if (inetAddress.isSiteLocalAddress())
return "site_local_" + ipVersion;
if (inetAddress.isLinkLocalAddress())
return "link_local_" + ipVersion;
if (inetAddress.isLoopbackAddress())
return "loopback_" + ipVersion;
return "public_" + ipVersion;
}
catch (final IllegalArgumentException e)
{
return "illegal_ip";
}
}
示例15: filterIP
import java.net.InetAddress; //导入方法依赖的package包/类
public static String filterIP(final InetAddress inetAddress)
{
try
{
final String ipVersion;
if (inetAddress instanceof Inet4Address)
ipVersion = "ipv4";
else if (inetAddress instanceof Inet6Address)
ipVersion = "ipv6";
else
ipVersion = "ipv?";
if (inetAddress.isAnyLocalAddress())
return "wildcard";
if (inetAddress.isSiteLocalAddress())
return "site_local_" + ipVersion;
if (inetAddress.isLinkLocalAddress())
return "link_local_" + ipVersion;
if (inetAddress.isLoopbackAddress())
return "loopback_" + ipVersion;
return inetAddress.getHostAddress();
}
catch (final IllegalArgumentException e)
{
return "illegal_ip";
}
}