本文整理汇总了Java中java.net.InetAddress.isSiteLocalAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.isSiteLocalAddress方法的具体用法?Java InetAddress.isSiteLocalAddress怎么用?Java InetAddress.isSiteLocalAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.isSiteLocalAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLocalIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private String getLocalIpAddress() {
String ipAddrrss = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
Enumeration<InetAddress> enumInetAddress = enumNetworkInterfaces.nextElement().getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ipAddrrss = ipAddrrss + inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipAddrrss;
}
示例2: getIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
@ReactMethod
public void getIpAddress(Callback successCallback, Callback errorCallback) {
WritableArray ipList = Arguments.createArray();
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ipList.pushString(inetAddress.getHostAddress());
}
}
}
} catch (SocketException e) {
Log.e(eTag, "getIpAddress SocketException", e);
errorCallback.invoke(e.getMessage());
}
successCallback.invoke(ipList);
}
示例3: addPublicAddress
import java.net.InetAddress; //导入方法依赖的package包/类
protected void
addPublicAddress(
Set<InetAddress> addresses,
InetAddress address )
{
if ( address == null ){
return;
}
if ( address.isAnyLocalAddress() ||
address.isLoopbackAddress() ||
address.isLinkLocalAddress()||
address.isSiteLocalAddress()){
return;
}
addresses.add( address );
}
示例4: getPublicIP4
import java.net.InetAddress; //导入方法依赖的package包/类
static public String getPublicIP4() {
Enumeration<NetworkInterface> ifs;
try {
ifs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
return null;
}
while (ifs.hasMoreElements()) {
NetworkInterface iface = ifs.nextElement();
Enumeration<InetAddress> iad = iface.getInetAddresses();
while (iad.hasMoreElements()) {
InetAddress localIP = iad.nextElement();
if (!localIP.isSiteLocalAddress() && !localIP.isLoopbackAddress()) {
// found an IPv4 address
if (localIP instanceof java.net.Inet4Address)
return localIP.getHostAddress();
}
}
}
return null;
}
示例5: extractRemoteIp
import java.net.InetAddress; //导入方法依赖的package包/类
private String extractRemoteIp(HttpServletRequest request) {
String forwardedHeader = request.getHeader("x-forwarded-for");
if (forwardedHeader != null) {
String[] addresses = forwardedHeader.split("[,]");
for (String address : addresses) {
try {
InetAddress inetAddress = InetAddress.getByName(address);
if (!inetAddress.isSiteLocalAddress()) {
return inetAddress.getHostAddress();
}
} catch (UnknownHostException e) {
LOGGER.debug("Failed to resolve IP for address: {}", address);
}
}
}
return request.getRemoteAddr();
}
示例6: initIpAdresses
import java.net.InetAddress; //导入方法依赖的package包/类
private List<String> initIpAdresses() {
List<String> result = new ArrayList<String>(2);
String ip = initIpAddress();
if (ip != null)
result.add(ip);
int intIpAddress = findDoubleNatIp();
if (intIpAddress != 0) {
byte[] bytes = BigInteger.valueOf(intIpAddress).toByteArray();
InetAddress address = null;
try {
address = InetAddress.getByAddress(bytes);
} catch (UnknownHostException ignored) {}
if (address != null && address.isSiteLocalAddress())
result.add(address.getHostAddress());
}
return result;
}
示例7: getOurIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private String getOurIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
示例8: getSiteLocalAddresses
import java.net.InetAddress; //导入方法依赖的package包/类
/** Returns all site-local scope (private) addresses for interfaces that are up. */
static InetAddress[] getSiteLocalAddresses() throws SocketException {
List<InetAddress> list = new ArrayList<>();
for (NetworkInterface intf : getInterfaces()) {
if (intf.isUp()) {
for (InetAddress address : Collections.list(intf.getInetAddresses())) {
if (address.isSiteLocalAddress()) {
list.add(address);
}
}
}
}
if (list.isEmpty()) {
throw new IllegalArgumentException("No up-and-running site-local (private) addresses found, got " + getInterfaces());
}
return list.toArray(new InetAddress[list.size()]);
}
示例9: getGlobalAddresses
import java.net.InetAddress; //导入方法依赖的package包/类
/** Returns all global scope addresses for interfaces that are up. */
static InetAddress[] getGlobalAddresses() throws SocketException {
List<InetAddress> list = new ArrayList<>();
for (NetworkInterface intf : getInterfaces()) {
if (intf.isUp()) {
for (InetAddress address : Collections.list(intf.getInetAddresses())) {
if (address.isLoopbackAddress() == false &&
address.isSiteLocalAddress() == false &&
address.isLinkLocalAddress() == false) {
list.add(address);
}
}
}
}
if (list.isEmpty()) {
throw new IllegalArgumentException("No up-and-running global-scope (public) addresses found, got " + getInterfaces());
}
return list.toArray(new InetAddress[list.size()]);
}
示例10: getRealIp
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getRealIp() {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;//是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& !ip.getHostAddress().contains(":")) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& !ip.getHostAddress().contains(":")) {// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
} catch (SocketException e) {
logger.error("获取本机Ip失败:异常信息:" + e.getMessage());
throw new RuntimeException(e);
}
}
示例11: onLogin
import java.net.InetAddress; //导入方法依赖的package包/类
@EventHandler
public void onLogin(PlayerLoginEvent event) throws UnknownHostException {
String host = event.getHostname();
String addrStr = host.substring(0, host.indexOf(':'));
InetAddress addr = InetAddress.getByName(addrStr);
if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress()) {
MCLibrary.setAddress(host);
event.getHandlers().unregister(this);
}
}
示例12: formatAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/** format internet address: java's default doesn't include everything useful */
private static String formatAddress(InterfaceAddress interfaceAddress) throws IOException {
StringBuilder sb = new StringBuilder();
InetAddress address = interfaceAddress.getAddress();
if (address instanceof Inet6Address) {
sb.append("inet6 ");
sb.append(NetworkAddress.format(address));
sb.append(" prefixlen:");
sb.append(interfaceAddress.getNetworkPrefixLength());
} else {
sb.append("inet ");
sb.append(NetworkAddress.format(address));
int netmask = 0xFFFFFFFF << (32 - interfaceAddress.getNetworkPrefixLength());
sb.append(" netmask:" + NetworkAddress.format(InetAddress.getByAddress(new byte[] {
(byte)(netmask >>> 24),
(byte)(netmask >>> 16 & 0xFF),
(byte)(netmask >>> 8 & 0xFF),
(byte)(netmask & 0xFF)
})));
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
sb.append(" broadcast:" + NetworkAddress.format(broadcast));
}
}
if (address.isLoopbackAddress()) {
sb.append(" scope:host");
} else if (address.isLinkLocalAddress()) {
sb.append(" scope:link");
} else if (address.isSiteLocalAddress()) {
sb.append(" scope:site");
}
return sb.toString();
}
示例13: scrubInetAddress
import java.net.InetAddress; //导入方法依赖的package包/类
@Nullable
public static String scrubInetAddress(InetAddress address) {
// don't scrub link and site local addresses
if (address.isLinkLocalAddress() || address.isSiteLocalAddress())
return address.toString();
// completely scrub IPv6 addresses
if (address instanceof Inet6Address) return "[scrubbed]";
// keep first and last octet of IPv4 addresses
return scrubInetAddress(address.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: formatAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/** format internet address: java's default doesn't include everything useful */
private static String formatAddress(InterfaceAddress interfaceAddress) throws IOException {
StringBuilder sb = new StringBuilder();
InetAddress address = interfaceAddress.getAddress();
if (address instanceof Inet6Address) {
sb.append("inet6 ");
sb.append(NetworkAddress.formatAddress(address));
sb.append(" prefixlen:");
sb.append(interfaceAddress.getNetworkPrefixLength());
} else {
sb.append("inet ");
sb.append(NetworkAddress.formatAddress(address));
int netmask = 0xFFFFFFFF << (32 - interfaceAddress.getNetworkPrefixLength());
sb.append(" netmask:" + NetworkAddress.formatAddress(InetAddress.getByAddress(new byte[] {
(byte)(netmask >>> 24),
(byte)(netmask >>> 16 & 0xFF),
(byte)(netmask >>> 8 & 0xFF),
(byte)(netmask & 0xFF)
})));
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
sb.append(" broadcast:" + NetworkAddress.formatAddress(broadcast));
}
}
if (address.isLoopbackAddress()) {
sb.append(" scope:host");
} else if (address.isLinkLocalAddress()) {
sb.append(" scope:link");
} else if (address.isSiteLocalAddress()) {
sb.append(" scope:site");
}
return sb.toString();
}