本文整理汇总了Java中java.net.InetAddress.isLoopbackAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.isLoopbackAddress方法的具体用法?Java InetAddress.isLoopbackAddress怎么用?Java InetAddress.isLoopbackAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.isLoopbackAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOneIPV4
import java.net.InetAddress; //导入方法依赖的package包/类
/** 获得IP信息 */
public static String getOneIPV4() {
ArrayList<String> list = new ArrayList<String>();
String result = "ip get failed";
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (inetAddress instanceof Inet4Address) {
if (!inetAddress.isLoopbackAddress()) {
list.add(inetAddress.getHostAddress());
}
}
}
}
} catch (SocketException ex) {
}
if (list.size() > 0)
result = list.get(0);
return result;
}
示例2: getLocalIpAddressV4
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getLocalIpAddressV4() {
String ip = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
//if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) //这里做了一步IPv4的判定
if (!inetAddress.isLoopbackAddress()
&& inetAddress instanceof Inet4Address) {
ip = inetAddress.getHostAddress().toString();
return ip;
}
}
}
} catch (SocketException e) {
// Log.i("SocketException--->", ""+e.getLocalizedMessage());
return "ip is error";
}
return ip;
}
示例3: getGPRSIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* 获取GPRS连接的情况的IP
*
* @return
*/
private static String getGPRSIpAddress()
{
String ip = "";
try
{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress() != null)
{
ip = inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException ex)
{
Log.e("WifiPreference IpAddress", ex.toString());
}
return ip == null ? "" : ip;
}
示例4: displayStackTraces
import java.net.InetAddress; //导入方法依赖的package包/类
public boolean displayStackTraces() {
ServletStackTraces mode = deployment.getDeploymentInfo().getServletStackTraces();
if (mode == ServletStackTraces.NONE) {
return false;
} else if (mode == ServletStackTraces.ALL) {
return true;
} else {
InetSocketAddress localAddress = getExchange().getSourceAddress();
if(localAddress == null) {
return false;
}
InetAddress address = localAddress.getAddress();
if(address == null) {
return false;
}
if(!address.isLoopbackAddress()) {
return false;
}
return !getExchange().getRequestHeaders().contains(Headers.X_FORWARDED_FOR);
}
}
示例5: getIp
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getIp() {
try {
for (Enumeration<NetworkInterface> enNetI = NetworkInterface
.getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
NetworkInterface netI = enNetI.nextElement();
for (Enumeration<InetAddress> enumIpAddr = netI
.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return "";
}
示例6: getDevicesIp
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* 获取设备IP
*
* @return
*/
public String getDevicesIp() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
示例7: getMobileIP
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* 获取GSM网络的IP地址
*/
public static String getMobileIP() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress = inetAddress.getHostAddress().toString();
return ipaddress;
}
}
}
} catch (SocketException ex) {
Log.e("getMobileIP", "Exception in Get IP Address: " + ex.toString());
}
return "";
}
示例8: getLocalIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return "0.0.0.0";
}
示例9: getLocalIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* getLocalIpAddress
* @return
*/
public static String getLocalIpAddress() {
String ret = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ret = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return ret;
}
示例10: isValidAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private static boolean isValidAddress(InetAddress address) {
if(address != null && !address.isLoopbackAddress()) {
String name = address.getHostAddress();
return name != null &&!LOCAL_IP_PATTERN.matcher(name).matches()&&!name.equalsIgnoreCase("localhost")&& !"0.0.0.0".equals(name) && !"192.168.122.1".equals(name)&& !"127.0.0.1".equals(name) && IP_PATTERN.matcher(name).matches();
} else {
return false;
}
}
示例11: isLocal
import java.net.InetAddress; //导入方法依赖的package包/类
public boolean isLocal() {
InetAddress a = address.getAddress();
if (a.isLinkLocalAddress() || a.isLoopbackAddress() || a.isSiteLocalAddress()) {
log.error("Loopback address: {}", a);
return true;
}
return false;
}
示例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: isValidAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private static boolean isValidAddress(InetAddress address) {
if (address == null || address.isLoopbackAddress())
return false;
String name = address.getHostAddress();
return (name != null
&& ! ANYHOST.equals(name)
&& ! LOCALHOST.equals(name)
&& IP_PATTERN.matcher(name).matches());
}
示例14: getIPAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* 获取IP地址
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
*
* @param useIPv4 是否用IPv4
* @return IP地址
*/
public static String getIPAddress(boolean useIPv4) {
try {
for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) {
NetworkInterface ni = nis.nextElement();
// 防止小米手机返回10.0.2.15
if (!ni.isUp())
continue;
for (Enumeration<InetAddress> addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) {
InetAddress inetAddress = addresses.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String hostAddress = inetAddress.getHostAddress();
boolean isIPv4 = hostAddress.indexOf(':') < 0;
if (useIPv4) {
if (isIPv4)
return hostAddress;
} else {
if (!isIPv4) {
int index = hostAddress.indexOf('%');
return index < 0 ? hostAddress.toUpperCase() : hostAddress.substring(0, index).toUpperCase();
}
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
示例15: getIPAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* 获取IP地址
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
*
* @param useIPv4 是否用IPv4
* @return IP地址
*/
public static String getIPAddress(boolean useIPv4) {
try {
for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) {
NetworkInterface ni = nis.nextElement();
// 防止小米手机返回10.0.2.15
if (!ni.isUp()) continue;
for (Enumeration<InetAddress> addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) {
InetAddress inetAddress = addresses.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String hostAddress = inetAddress.getHostAddress();
boolean isIPv4 = hostAddress.indexOf(':') < 0;
if (useIPv4) {
if (isIPv4) return hostAddress;
} else {
if (!isIPv4) {
int index = hostAddress.indexOf('%');
return index < 0 ? hostAddress.toUpperCase() : hostAddress.substring(0, index).toUpperCase();
}
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}