类java.net.InterfaceAddress源码实例Demo

下面列出了怎么用java.net.InterfaceAddress的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: submarine   文件: NetworkUtils.java
public static String findAvailableHostAddress() throws UnknownHostException, SocketException {
  String submarineServerIP = System.getenv("SUBMARINE_LOCAL_IP");
  if (submarineServerIP != null) {
    return submarineServerIP;
  }

  InetAddress address = InetAddress.getLocalHost();
  if (address.isLoopbackAddress()) {
    for (NetworkInterface networkInterface : Collections
        .list(NetworkInterface.getNetworkInterfaces())) {
      if (!networkInterface.isLoopback()) {
        for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
          InetAddress a = interfaceAddress.getAddress();
          if (a instanceof Inet4Address) {
            return a.getHostAddress();
          }
        }
      }
    }
  }
  return address.getHostAddress();
}
 
源代码2 项目: PacketProxy   文件: PrivateDNS.java
SpoofAddrFactory() throws Exception {
	Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
	for (NetworkInterface netint : Collections.list(nets)) {
		for (InterfaceAddress intAddress : netint.getInterfaceAddresses()) {
			InetAddress addr = intAddress.getAddress();
			if (addr instanceof Inet4Address) {
				String cidr = String.format("%s/%d", addr.getHostAddress(), intAddress.getNetworkPrefixLength());
				SubnetUtils subnet = new SubnetUtils(cidr);
				subnets.add(subnet.getInfo());
				if (defaultAddr == null) {
					defaultAddr = addr.getHostAddress();
				} else if (defaultAddr.equals("127.0.0.1")) {
					defaultAddr = addr.getHostAddress();
				}
			}
		}
	}
}
 
源代码3 项目: smarthome   文件: NetUtil.java
/**
 * Get all broadcast addresses on the current host
 *
 * @return list of broadcast addresses, empty list if no broadcast addresses found
 */
public static List<String> getAllBroadcastAddresses() {
    List<String> broadcastAddresses = new LinkedList<String>();
    try {
        final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            final NetworkInterface networkInterface = networkInterfaces.nextElement();
            final List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
            for (InterfaceAddress interfaceAddress : interfaceAddresses) {
                final InetAddress addr = interfaceAddress.getAddress();
                if (addr instanceof Inet4Address && !addr.isLinkLocalAddress() && !addr.isLoopbackAddress()) {
                    InetAddress broadcast = interfaceAddress.getBroadcast();
                    if (broadcast != null) {
                        broadcastAddresses.add(broadcast.getHostAddress());
                    }
                }
            }
        }
    } catch (SocketException ex) {
        LOGGER.error("Could not find broadcast address: {}", ex.getMessage(), ex);
    }
    return broadcastAddresses;
}
 
源代码4 项目: dragonwell8_jdk   文件: NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();

    while (nics.hasMoreElements()) {
        NetworkInterface nic = nics.nextElement();
        for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
            boolean valid = checkPrefix(iaddr);
            if (!valid) {
                passed = false;
                debug(nic.getName(), iaddr);
            }
            InetAddress ia = iaddr.getAddress();
            if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                // assumption: prefix length will always be 8
                if (iaddr.getNetworkPrefixLength() != 8) {
                    out.println("Expected prefix of 8, got " + iaddr);
                    passed = false;
                }
            }
        }
    }

    if (!passed)
        throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
 
源代码5 项目: swingsane   文件: DiscoveryJob.java
public final InetAddress getLocalAddress() throws SocketException {
  for (final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
      .hasMoreElements();) {
    final NetworkInterface networkInterface = interfaces.nextElement();
    if (networkInterface.isLoopback()) {
      continue;
    }
    for (final InterfaceAddress interfaceAddr : networkInterface.getInterfaceAddresses()) {
      final InetAddress inetAddr = interfaceAddr.getAddress();
      if (!(inetAddr instanceof Inet4Address)) {
        continue;
      }
      return inetAddr;
    }
  }
  return null;
}
 
源代码6 项目: TencentKona-8   文件: NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();

    while (nics.hasMoreElements()) {
        NetworkInterface nic = nics.nextElement();
        for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
            boolean valid = checkPrefix(iaddr);
            if (!valid) {
                passed = false;
                debug(nic.getName(), iaddr);
            }
            InetAddress ia = iaddr.getAddress();
            if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                // assumption: prefix length will always be 8
                if (iaddr.getNetworkPrefixLength() != 8) {
                    out.println("Expected prefix of 8, got " + iaddr);
                    passed = false;
                }
            }
        }
    }

    if (!passed)
        throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
 
源代码7 项目: g4proxy   文件: NetworkUtils.java
/**
 * @deprecated This method is no longer used by LittleProxy and may be removed in a future release.
 */
@Deprecated
public static InetAddress firstLocalNonLoopbackIpv4Address() {
    try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
                .getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces
                    .nextElement();
            if (networkInterface.isUp()) {
                for (InterfaceAddress ifAddress : networkInterface
                        .getInterfaceAddresses()) {
                    if (ifAddress.getNetworkPrefixLength() > 0
                            && ifAddress.getNetworkPrefixLength() <= 32
                            && !ifAddress.getAddress().isLoopbackAddress()) {
                        return ifAddress.getAddress();
                    }
                }
            }
        }
        return null;
    } catch (SocketException se) {
        return null;
    }
}
 
源代码8 项目: orWall   文件: NetworkHelper.java
public static String getMask(String intf){
    try {
        NetworkInterface ntwrk = NetworkInterface.getByName(intf);
        Iterator<InterfaceAddress> addrList = ntwrk.getInterfaceAddresses().iterator();
        while (addrList.hasNext()) {
            InterfaceAddress addr = addrList.next();
            InetAddress ip = addr.getAddress();
            if (ip instanceof Inet4Address) {
                String mask = ip.getHostAddress() + "/" +
                        addr.getNetworkPrefixLength();
                return mask;
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}
 
源代码9 项目: DevUtils   文件: NetWorkUtils.java
/**
 * 获取广播 IP 地址
 * @return 广播 IP 地址
 */
public static String getBroadcastIpAddress() {
    try {
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        while (nis.hasMoreElements()) {
            NetworkInterface ni = nis.nextElement();
            if (!ni.isUp() || ni.isLoopback()) continue;
            List<InterfaceAddress> ias = ni.getInterfaceAddresses();
            for (int i = 0; i < ias.size(); i++) {
                InterfaceAddress ia = ias.get(i);
                InetAddress broadcast = ia.getBroadcast();
                if (broadcast != null) {
                    return broadcast.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getBroadcastIpAddress");
    }
    return null;
}
 
源代码10 项目: openhab1-addons   文件: SsdpDiscovery.java
private static List<InetAddress> getBroadCastAddress() throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    List<InetAddress> addresses = new ArrayList<InetAddress>();
    addresses.add(InetAddress.getByName("255.255.255.255"));
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        if (networkInterface.isLoopback() || !networkInterface.supportsMulticast()) {
            continue; // Don't want to broadcast to the loopback interface
        }
        for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
            InetAddress broadcast = interfaceAddress.getBroadcast();
            if (broadcast != null) {
                addresses.add(broadcast);
            }
        }
    }
    return addresses;
}
 
源代码11 项目: openjdk-jdk8u   文件: NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();

    while (nics.hasMoreElements()) {
        NetworkInterface nic = nics.nextElement();
        for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
            boolean valid = checkPrefix(iaddr);
            if (!valid) {
                passed = false;
                debug(nic.getName(), iaddr);
            }
            InetAddress ia = iaddr.getAddress();
            if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                // assumption: prefix length will always be 8
                if (iaddr.getNetworkPrefixLength() != 8) {
                    out.println("Expected prefix of 8, got " + iaddr);
                    passed = false;
                }
            }
        }
    }

    if (!passed)
        throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
 
源代码12 项目: Smack   文件: Socks5Proxy.java
/**
 * Private constructor.
 */
Socks5Proxy() {
    this.serverProcess = new Socks5ServerProcess();

    allowAllConnections = false;

    Enumeration<NetworkInterface> networkInterfaces;
    try {
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        throw new IllegalStateException(e);
    }
    Set<InetAddress> localAddresses = new HashSet<>();
    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
        for (InterfaceAddress interfaceAddress : interfaceAddresses) {
            localAddresses.add(interfaceAddress.getAddress());
        }
    }
    if (localAddresses.isEmpty()) {
        throw new IllegalStateException("Could not determine any local internet address");
    }
    replaceLocalAddresses(localAddresses);
}
 
源代码13 项目: ForgePE   文件: MainActivity.java
public String[] getBroadcastAddresses() {
    ArrayList<String> list = new ArrayList<>();
    try {
        System.setProperty("java.net.preferIPv4Stack", "true");
        Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();

        while (niEnum.hasMoreElements()) {
            NetworkInterface ni = niEnum.nextElement();

            if (!ni.isLoopback()) {
                for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                    if (interfaceAddress.getBroadcast() != null)
                        list.add(interfaceAddress.getBroadcast().toString().substring(1));
                }
            }
        }
    } catch (Exception ignored) {
    }
    return list.toArray(new String[list.size()]);
}
 
源代码14 项目: incubator-crail   文件: StorageUtils.java
public static InetSocketAddress getDataNodeAddress(String ifname, int port) throws IOException {
	NetworkInterface netif = NetworkInterface.getByName(ifname);
	if (netif == null){
		throw new IOException("Cannot find network interface with name " + ifname);
	}
	List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
	InetAddress addr = null;
	for (InterfaceAddress address: addresses){
		if (address.getBroadcast() != null){
			InetAddress _addr = address.getAddress();
			addr = _addr;
		}
	}

	if (addr == null){
		throw new IOException("Network interface with name " + ifname + " has no valid IP address");
	}
	InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
	return inetAddr;
}
 
源代码15 项目: AndroidDemo   文件: Tools.java
public static String getBroadcast() {
    System.setProperty("java.net.preferIPv4Stack", "true");
    try {
        for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum.hasMoreElements(); ) {
            NetworkInterface ni = niEnum.nextElement();
            if (ni.getName().toLowerCase().equals("wlan0")) {
                if (!ni.isLoopback()) {
                    for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                        if (interfaceAddress.getBroadcast() != null) {
                            return interfaceAddress.getBroadcast().toString().substring(1);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        //
    }
    return null;
}
 
源代码16 项目: logstash   文件: NetworkUtils.java
private InetAddress getLocalAddress(String adaptorName){
    try {
        Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
        while (b.hasMoreElements()) {
            NetworkInterface networkInterface = b.nextElement();
            if (networkInterface.getName().equals(adaptorName)) {
                for (InterfaceAddress f : networkInterface.getInterfaceAddresses()) {
                    if (f.getAddress().isSiteLocalAddress()) {
                        return f.getAddress();
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}
 
源代码17 项目: cloudstack   文件: NetUtils.java
public static List<String> getAllDefaultNicIps() {
    final List<String> addrs = new ArrayList<>();
    final String pubNic = getDefaultEthDevice();

    if (pubNic == null) {
        return addrs;
    }

    NetworkInterface nic = null;
    try {
        nic = NetworkInterface.getByName(pubNic);
    } catch (final SocketException e) {
        return addrs;
    }

    for (InterfaceAddress address : nic.getInterfaceAddresses()) {
        addrs.add(address.getAddress().getHostAddress().split("%")[0]);
    }
    return addrs;
}
 
源代码18 项目: kurento-java   文件: Docker.java
public String getHostIpForContainers() {
  try {
    Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
    while (b.hasMoreElements()) {
      NetworkInterface iface = b.nextElement();
      if (iface.getName().contains("docker")) {
        for (InterfaceAddress f : iface.getInterfaceAddresses()) {
          if (f.getAddress().isSiteLocalAddress()) {
            String addr = f.getAddress().toString();
            log.debug("Host IP for container is {}", addr);
            return addr;
          }
        }
      }
    }
  } catch (SocketException e) {
    // This shouldn't happen
    log.warn("Exception getting docker address", e);
  }

  return null;
}
 
源代码19 项目: deeplearning4j   文件: VoidParameterServer.java
/**
 * This method returns set of local IP addresses available in system.
 *
 * PLEASE NOTE: loopback, disabled interfaces, IPv6 addresses are ignored here.
 *
 * @return
 */
public static Set<String> getLocalAddresses() {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

        Set<String> result = new HashSet<>();

        for (NetworkInterface networkInterface : interfaces) {
            if (networkInterface.isLoopback() || !networkInterface.isUp())
                continue;

            for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
                String addr = address.getAddress().getHostAddress();

                if (addr == null || addr.isEmpty() || addr.contains(":"))
                    continue;

                result.add(addr);
            }
        }

        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
源代码20 项目: WifiChat   文件: WifiUtils.java
public static String getBroadcastAddress() {
    System.setProperty("java.net.preferIPv4Stack", "true");
    try {
        for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum
                .hasMoreElements();) {
            NetworkInterface ni = niEnum.nextElement();
            if (!ni.isLoopback()) {
                for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                    if (interfaceAddress.getBroadcast() != null) {
                        logger.d(interfaceAddress.getBroadcast().toString().substring(1));
                        return interfaceAddress.getBroadcast().toString().substring(1);
                    }
                }
            }
        }
    }
    catch (SocketException e) {
        e.printStackTrace();
    }

    return null;
}
 
源代码21 项目: openjdk-jdk9   文件: NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();

    while (nics.hasMoreElements()) {
        NetworkInterface nic = nics.nextElement();
        for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
            boolean valid = checkPrefix(iaddr);
            if (!valid) {
                passed = false;
                debug(nic.getName(), iaddr);
            }
            InetAddress ia = iaddr.getAddress();
            if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                // assumption: prefix length will always be 8
                if (iaddr.getNetworkPrefixLength() != 8) {
                    out.println("Expected prefix of 8, got " + iaddr);
                    passed = false;
                }
            }
        }
    }

    if (!passed)
        throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
 
源代码22 项目: DroidDLNA   文件: NetworkAddressFactoryImpl.java
protected InetAddress getBindAddressInSubnetOf(InetAddress inetAddress) {
    synchronized (networkInterfaces) {
        for (NetworkInterface iface : networkInterfaces) {
            for (InterfaceAddress ifaceAddress : getInterfaceAddresses(iface)) {

                synchronized (bindAddresses) {
                    if (ifaceAddress == null || !bindAddresses.contains(ifaceAddress.getAddress())) {
                        continue;
                    }
                }

                if (isInSubnet(
                        inetAddress.getAddress(),
                        ifaceAddress.getAddress().getAddress(),
                        ifaceAddress.getNetworkPrefixLength())
                        ) {
                    return ifaceAddress.getAddress();
                }
            }

        }
    }
    return null;
}
 
源代码23 项目: openjdk-8-source   文件: NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();

    while (nics.hasMoreElements()) {
        NetworkInterface nic = nics.nextElement();
        for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
            boolean valid = checkPrefix(iaddr);
            if (!valid) {
                passed = false;
                debug(nic.getName(), iaddr);
            }
            InetAddress ia = iaddr.getAddress();
            if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                // assumption: prefix length will always be 8
                if (iaddr.getNetworkPrefixLength() != 8) {
                    out.println("Expected prefix of 8, got " + iaddr);
                    passed = false;
                }
            }
        }
    }

    if (!passed)
        throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
 
源代码24 项目: deeplearning4j   文件: NetworkOrganizer.java
protected List<NetworkInformation> buildLocalInformation() {
    List<NetworkInformation> list = new ArrayList<>();
    NetworkInformation netInfo = new NetworkInformation();
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

        for (NetworkInterface networkInterface : interfaces) {
            if (!networkInterface.isUp())
                continue;

            for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
                String addr = address.getAddress().getHostAddress();

                if (addr == null || addr.isEmpty() || addr.contains(":"))
                    continue;

                netInfo.getIpAddresses().add(addr);
            }
        }
        list.add(netInfo);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return list;
}
 
源代码25 项目: jdk8u-jdk   文件: NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();

    while (nics.hasMoreElements()) {
        NetworkInterface nic = nics.nextElement();
        for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
            boolean valid = checkPrefix(iaddr);
            if (!valid) {
                passed = false;
                debug(nic.getName(), iaddr);
            }
            InetAddress ia = iaddr.getAddress();
            if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                // assumption: prefix length will always be 8
                if (iaddr.getNetworkPrefixLength() != 8) {
                    out.println("Expected prefix of 8, got " + iaddr);
                    passed = false;
                }
            }
        }
    }

    if (!passed)
        throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
 
源代码26 项目: jdk8u_jdk   文件: NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();

    while (nics.hasMoreElements()) {
        NetworkInterface nic = nics.nextElement();
        for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
            boolean valid = checkPrefix(iaddr);
            if (!valid) {
                passed = false;
                debug(nic.getName(), iaddr);
            }
            InetAddress ia = iaddr.getAddress();
            if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                // assumption: prefix length will always be 8
                if (iaddr.getNetworkPrefixLength() != 8) {
                    out.println("Expected prefix of 8, got " + iaddr);
                    passed = false;
                }
            }
        }
    }

    if (!passed)
        throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
 
源代码27 项目: xDrip-plus   文件: RouteTools.java
static List<InterfaceAddress> getLikelyInterfaceAddresses() {
    final List<InterfaceAddress> results = new ArrayList<>();
    try {
        final List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface face : interfaces) {
            final List<InterfaceAddress> interfaceAddresses = face.getInterfaceAddresses();
            //  for (final InterfaceAddress adx : interfaceAddresses) {
            //      android.util.Log.d("DesertComms", "addr: " + adx.getAddress().getHostAddress() + " " + adx.getNetworkPrefixLength());
            //  }
            results.addAll(interfaceAddresses);
        }
    } catch (Exception ignored) {
        //
    }
    return results;
}
 
源代码28 项目: j2objc   文件: NetworkInterfaceTest.java
public void testInterfaceProperties() throws Exception {
    for (NetworkInterface nif : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        assertEquals(nif, NetworkInterface.getByName(nif.getName()));
        // Skip interfaces that are inactive
        if (nif.isUp() == false) {
            continue;
        }
        // Ethernet
        if (isEthernet(nif.getName())) {
            assertEquals(6, nif.getHardwareAddress().length);
            for (InterfaceAddress ia : nif.getInterfaceAddresses()) {
                if (ia.getAddress() instanceof Inet4Address) {
                    assertNotNull(ia.getBroadcast());
                }
            }
        }
    }
}
 
源代码29 项目: qpid-jms   文件: MulticastDiscoveryAgent.java
private static List<NetworkInterface> findNetworkInterfaces() throws SocketException {
    Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
    List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
    while (ifcs.hasMoreElements()) {
        NetworkInterface ni = ifcs.nextElement();
        LOG.trace("findNetworkInterfaces checking interface: {}", ni);

        if (ni.supportsMulticast() && ni.isUp()) {
            for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
                if (ia.getAddress() instanceof java.net.Inet4Address &&
                    !ia.getAddress().isLoopbackAddress() &&
                    !DEFAULT_EXCLUSIONS.contains(ni.getName())) {
                    // Add at the start, make usage order consistent with the
                    // existing ActiveMQ releases discovery will be used with.
                    interfaces.add(0, ni);
                    break;
                }
            }
        }
    }

    LOG.trace("findNetworkInterfaces returning: {}", interfaces);

    return interfaces;
}
 
源代码30 项目: AndroidUtilCode   文件: NetworkUtils.java
/**
 * Return the ip address of broadcast.
 *
 * @return the ip address of broadcast
 */
public static String getBroadcastIpAddress() {
    try {
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        LinkedList<InetAddress> adds = new LinkedList<>();
        while (nis.hasMoreElements()) {
            NetworkInterface ni = nis.nextElement();
            if (!ni.isUp() || ni.isLoopback()) continue;
            List<InterfaceAddress> ias = ni.getInterfaceAddresses();
            for (int i = 0, size = ias.size(); i < size; i++) {
                InterfaceAddress ia = ias.get(i);
                InetAddress broadcast = ia.getBroadcast();
                if (broadcast != null) {
                    return broadcast.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "";
}
 
 类所在包
 同包方法