当前位置: 首页>>代码示例>>Java>>正文


Java NetworkInterface.isUp方法代码示例

本文整理汇总了Java中java.net.NetworkInterface.isUp方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.isUp方法的具体用法?Java NetworkInterface.isUp怎么用?Java NetworkInterface.isUp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.NetworkInterface的用法示例。


在下文中一共展示了NetworkInterface.isUp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
public String getIp() {
    String id = "-1";
    try {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                continue;
            } else {
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        id = ip.getHostAddress();
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("Cannot get Id, something wrong with getting Ipv4 address");
    }
    return id;
}
 
开发者ID:xiongbeer,项目名称:Cobweb,代码行数:25,代码来源:IdProvider.java

示例2: formatFlags

import java.net.NetworkInterface; //导入方法依赖的package包/类
/** format network interface flags */
private static String formatFlags(NetworkInterface nic) throws SocketException {
    StringBuilder flags = new StringBuilder();
    if (nic.isUp()) {
        flags.append("UP ");
    }
    if (nic.supportsMulticast()) {
        flags.append("MULTICAST ");
    }
    if (nic.isLoopback()) {
        flags.append("LOOPBACK ");
    }
    if (nic.isPointToPoint()) {
        flags.append("POINTOPOINT ");
    }
    if (nic.isVirtual()) {
        flags.append("VIRTUAL ");
    }
    flags.append("mtu:" + nic.getMTU());
    flags.append(" index:" + nic.getIndex());
    return flags.toString();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:23,代码来源:IfConfig.java

示例3: getSiteLocalAddresses

import java.net.NetworkInterface; //导入方法依赖的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()]);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:18,代码来源:NetworkUtils.java

示例4: getGlobalAddresses

import java.net.NetworkInterface; //导入方法依赖的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()]);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:20,代码来源:NetworkUtils.java

示例5: getLocalAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * 获取本机的IP
 * 
 * @return Ip地址
 */
private static String getLocalAddress() {
       try {
           for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
               NetworkInterface networkInterface = interfaces.nextElement();
               if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                   continue;
               }
               Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
               if (addresses.hasMoreElements()) {
               	InetAddress address = addresses.nextElement();
               	if(address instanceof Inet4Address) {
               		return address.getHostAddress();
               	}
               }
           }
       } catch (SocketException e) {
           logger.debug("Error when getting host ip address: <{}>.", e.getMessage());
       }
       return "127.0.0.1";
   }
 
开发者ID:xtuhcy,项目名称:socks5-netty,代码行数:26,代码来源:ProxyFlowLog4j.java

示例6: findPublicAddresses

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Find all ipv4 and ipv6 addresses on the local host that can be used for sending packets to the internet
 * @return
 * @throws IOException
 */
public List<InetAddress> findPublicAddresses() throws IOException {
	List<InetAddress> result = new ArrayList<InetAddress>();
	Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
	while (networkInterfaces.hasMoreElements()) {
		NetworkInterface networkInterface = networkInterfaces.nextElement();
		if (!networkInterface.isLoopback() && networkInterface.isUp()) {
			Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
			while (inetAddresses.hasMoreElements()) {
				InetAddress inetAddress = inetAddresses.nextElement();
				if (!inetAddress.isLinkLocalAddress()) {
					result.add(inetAddress);
				}
			}
		}
	}
	return result;
}
 
开发者ID:xtianus,项目名称:yadaframework,代码行数:23,代码来源:YadaNetworkUtil.java

示例7: findLoopbackInterface

import java.net.NetworkInterface; //导入方法依赖的package包/类
private NetworkInterface findLoopbackInterface(InetAddress address) throws SocketException, HekateException {
    NetworkInterface lo = null;

    for (NetworkInterface nif : AddressUtils.activeNetworks()) {
        if (nif.isUp() && nif.isLoopback()) {
            for (InetAddress nifAddress : Collections.list(nif.getInetAddresses())) {
                if (!nifAddress.isLinkLocalAddress() && nifAddress.isLoopbackAddress()) {
                    if (lo != null) {
                        throw new HekateException("Failed to resolve a loopback network interface. "
                            + "Multiple loopback interfaces were detected [address=" + address + ", interface1=" + lo.getName()
                            + ", interface2=" + nif.getName() + ']');
                    }

                    lo = nif;

                    break;
                }
            }
        }
    }

    return lo;
}
 
开发者ID:hekate-io,项目名称:hekate,代码行数:24,代码来源:MulticastSeedNodeProvider.java

示例8: activeNetworks

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Returns an immutable list of all {@link NetworkInterface#isUp() active} network interfaces.
 *
 * @return List of all {@link NetworkInterface#isUp() active} network interfaces.
 *
 * @throws SocketException If network interfaces couldn't be loaded.
 * @see NetworkInterface#getNetworkInterfaces()
 */
public static List<NetworkInterface> activeNetworks() throws SocketException {
    NetCacheEntry cache = NET_CACHE.get();

    if (cache == null || cache.getTimestamp() < System.currentTimeMillis() - NET_CACHE_TIMEOUT) {
        ArrayList<NetworkInterface> all = list(NetworkInterface.getNetworkInterfaces());

        List<NetworkInterface> active = new ArrayList<>(all.size());

        for (NetworkInterface net : all) {
            if (net.isUp()) {
                active.add(net);
            }
        }

        cache = new NetCacheEntry(System.currentTimeMillis(), unmodifiableList(active));

        NET_CACHE.set(cache);
    }

    return cache.getInterfaces();
}
 
开发者ID:hekate-io,项目名称:hekate,代码行数:30,代码来源:AddressUtils.java

示例9: getIPAddress

import java.net.NetworkInterface; //导入方法依赖的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;
}
 
开发者ID:StickyTolt,项目名称:ForeverLibrary,代码行数:35,代码来源:NetworkUtils.java

示例10: getIPAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * 获取IP地址
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
 *
 * @param useIPv4 是否用IPv4
 * @return IP地址
 */
public static String getIPAddress(final 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;
}
 
开发者ID:Wilshion,项目名称:HeadlineNews,代码行数:35,代码来源:NetworkUtils.java

示例11: getIPs

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getIPs()
{
	try
	{
		Enumeration<NetworkInterface> nets;
		NetworkInterface n;
		Enumeration<InetAddress> addrs;
		InetAddress a;

		String s = "";
		nets = NetworkInterface.getNetworkInterfaces();
		while (nets.hasMoreElements())
		{
			n = nets.nextElement();
			if (n.isUp())
			{
				s += n.getName() + "\n";
				addrs = n.getInetAddresses();
				while (addrs.hasMoreElements())
				{
					a = addrs.nextElement();
					if (a instanceof Inet4Address)
					{
						s += " * " + a.getHostAddress() + "\n";
					}
				}
			}
		}

		return s.trim();
	}
	catch (SocketException e)
	{
		System.out.println("Error: couldn't get ip addresses for network interfaces");
		return "";
	}
}
 
开发者ID:D4RK0V3RL0RD676,项目名称:apcs_final,代码行数:38,代码来源:LANUtils.java

示例12: getAllAddresses

import java.net.NetworkInterface; //导入方法依赖的package包/类
/** Returns all addresses (any scope) for interfaces that are up. 
 *  This is only used to pick a publish address, when the user set network.host to a wildcard */
static InetAddress[] getAllAddresses() throws SocketException {
    List<InetAddress> list = new ArrayList<>();
    for (NetworkInterface intf : getInterfaces()) {
        if (intf.isUp()) {
            for (InetAddress address : Collections.list(intf.getInetAddresses())) {
                list.add(address);
            }
        }
    }
    if (list.isEmpty()) {
        throw new IllegalArgumentException("No up-and-running addresses found, got " + getInterfaces());
    }
    return list.toArray(new InetAddress[list.size()]);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:17,代码来源:NetworkUtils.java

示例13: testAddressInterfaceLookup

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Test that selecting by name is possible and properly matches the addresses on all interfaces and virtual
 * interfaces.
 *
 * Note that to avoid that this test fails when interfaces are down or they do not have addresses assigned to them,
 * they are ignored.
 */
public void testAddressInterfaceLookup() throws Exception {
    for (NetworkInterface netIf : NetworkUtils.getInterfaces()) {
        if (!netIf.isUp() || Collections.list(netIf.getInetAddresses()).isEmpty()) {
            continue;
        }

        String name = netIf.getName();
        InetAddress[] expectedAddresses = Collections.list(netIf.getInetAddresses()).toArray(new InetAddress[0]);
        InetAddress[] foundAddresses = NetworkUtils.getAddressesForInterface(name);
        assertArrayEquals(expectedAddresses, foundAddresses);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:20,代码来源:NetworkUtilsTests.java

示例14: getInetAddresses

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static final ArrayList<InetAddress> getInetAddresses(InetAddressFilter inetAddressFilter) {
    try {
        final ArrayList<InetAddress> inetAddresses = new ArrayList<>();
        final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            final NetworkInterface networkInterface = networkInterfaces.nextElement();
            if (networkInterface == null || networkInterface.isLoopback() || !networkInterface.isUp() || networkInterface.isVirtual() || networkInterface.isPointToPoint()) {
                continue;
            }
            final Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                final InetAddress inetAddress = addresses.nextElement();
                if (inetAddress == null) {
                    continue;
                }
                if (inetAddressFilter.filter(inetAddress)) {
                    inetAddresses.add(inetAddress);
                }
            }
        }
        return inetAddresses;
    } catch (Exception ex) {
        System.err.println("NetworkUtil: getInetAddresses error");
        ex.printStackTrace();
        return new ArrayList<>();
    }
}
 
开发者ID:Panzer1119,项目名称:Supreme-Bot,代码行数:28,代码来源:NetworkUtil.java

示例15: getIPAddress

import java.net.NetworkInterface; //导入方法依赖的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;
}
 
开发者ID:ChunweiDu,项目名称:Utils,代码行数:37,代码来源:NetworkUtil.java


注:本文中的java.net.NetworkInterface.isUp方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。