getBroadcast ( )源码实例Demo

java.net.InterfaceAddress#getBroadcast ( )源码实例Demo

下面列出了java.net.InterfaceAddress#getBroadcast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: 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;
}
 
源代码2 项目: 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;
}
 
源代码3 项目: 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;
}
 
源代码4 项目: samba-documents-provider   文件: BroadcastUtils.java
static List<String> getBroadcastAddress() throws BrowsingException, SocketException {
  Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

  List<String> broadcastAddresses = new ArrayList<>();

  while (interfaces.hasMoreElements()) {
    NetworkInterface networkInterface = interfaces.nextElement();
    if (networkInterface.isLoopback()) {
      continue;
    }

    for (InterfaceAddress interfaceAddress :
            networkInterface.getInterfaceAddresses()) {
      InetAddress broadcast = interfaceAddress.getBroadcast();

      if (broadcast != null) {
        broadcastAddresses.add(broadcast.toString().substring(1));
      }
    }
  }

  return broadcastAddresses;
}
 
源代码5 项目: 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 "";
}
 
源代码6 项目: crail   文件: TcpStorageServer.java
public static InetSocketAddress getDataNodeAddress() throws IOException {
	String ifname = TcpStorageConstants.STORAGE_TCP_INTERFACE;
	int port = TcpStorageConstants.STORAGE_TCP_PORT;
	
	NetworkInterface netif = NetworkInterface.getByName(ifname);
	if (netif == null){
		return null;
	}
	List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
	InetAddress addr = null;
	for (InterfaceAddress address: addresses){
		if (address.getBroadcast() != null){
			InetAddress _addr = address.getAddress();
			addr = _addr;
		}
	}		
	InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
	return inetAddr;
}
 
源代码7 项目: crail   文件: RdmaStorageServer.java
public static InetSocketAddress getDataNodeAddress() throws IOException {
		String ifname = RdmaConstants.STORAGE_RDMA_INTERFACE;
		int port = RdmaConstants.STORAGE_RDMA_PORT;
		
		NetworkInterface netif = NetworkInterface.getByName(ifname);
		if (netif == null){
			return null;
		}
		List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
		InetAddress addr = null;
		for (InterfaceAddress address: addresses){
//			LOG.info("address* " + address.toString() + ", _addr " + _addr.toString() + ", isSiteLocal " + _addr.isSiteLocalAddress() + ", tmp " + tmp + ", size " + tmp.length + ", broadcast " + address.getBroadcast());
			if (address.getBroadcast() != null){
				InetAddress _addr = address.getAddress();
				addr = _addr;
			}
		}		
		InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
		return inetAddr;
	}
 
源代码8 项目: FlyWoo   文件: 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) {
                        L.d(TAG, interfaceAddress.getBroadcast().toString().substring(1));
                        return interfaceAddress.getBroadcast().toString().substring(1);
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    return null;
}
 
源代码9 项目: openhab-core   文件: 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<>();
    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;
}
 
源代码10 项目: 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()]);
}
 
源代码11 项目: 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;
}
 
源代码12 项目: deskcon-android   文件: DesktopHostsActivity.java
public static InetAddress getBroadcast(){
InetAddress found_bcast_address=null;
 System.setProperty("java.net.preferIPv4Stack", "true"); 
    try
    {
      Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
      while (niEnum.hasMoreElements())
      {
        NetworkInterface ni = niEnum.nextElement();
        if(!ni.isLoopback()){
            for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses())
            {
              found_bcast_address = interfaceAddress.getBroadcast();               
            }
        }
      }
    }
    catch (SocketException e)
    {
      e.printStackTrace();
    }

    return found_bcast_address;
}
 
源代码13 项目: 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;
}
 
源代码14 项目: 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;
}
 
源代码15 项目: TVRemoteIME   文件: NetworkAddressFactoryImpl.java
public InetAddress getBroadcastAddress(InetAddress inetAddress) {
    synchronized (networkInterfaces) {
        for (NetworkInterface iface : networkInterfaces) {
            for (InterfaceAddress interfaceAddress : getInterfaceAddresses(iface)) {
                if (interfaceAddress != null && interfaceAddress.getAddress().equals(inetAddress)) {
                    return interfaceAddress.getBroadcast();
                }
            }
        }
    }
    return null;
}
 
源代码16 项目: Elasticsearch   文件: IfConfig.java
/** 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();
}
 
源代码17 项目: moleculer-java   文件: UDPLocator.java
protected void startReceivers(NetworkInterface ni, String udpMulticast, boolean udpBroadcast) throws Exception {
	if (ni == null || ni.isLoopback()) {
		return;
	}
	List<InterfaceAddress> list = ni.getInterfaceAddresses();
	if (list == null || list.isEmpty()) {
		return;
	}
	if (udpMulticast != null && ni.supportsMulticast()) {

		// Create multicast receiver
		receivers.add(new UDPMulticastReceiver(nodeID, udpMulticast, transporter, ni));
	}
	if (udpBroadcast) {
		for (InterfaceAddress ia : list) {
			if (ia == null) {
				continue;
			}
			InetAddress address = ia.getBroadcast();
			if (address == null || address.isLoopbackAddress()) {
				continue;
			}
			String udpAddress = address.getHostAddress();
			if (udpAddress == null || udpAddress.isEmpty() || udpAddress.startsWith("127.")) {
				continue;
			}

			// Create broadcast receiver
			receivers.add(new UDPBroadcastReceiver(nodeID, udpAddress, transporter));
		}
	}
}
 
源代码18 项目: DroidDLNA   文件: NetworkAddressFactoryImpl.java
public InetAddress getBroadcastAddress(InetAddress inetAddress) {
    synchronized (networkInterfaces) {
        for (NetworkInterface iface : networkInterfaces) {
            for (InterfaceAddress interfaceAddress : getInterfaceAddresses(iface)) {
                if (interfaceAddress != null && interfaceAddress.getAddress().equals(inetAddress)) {
                    return interfaceAddress.getBroadcast();
                }
            }
        }
    }
    return null;
}
 
源代码19 项目: crate   文件: IfConfig.java
/** 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:").append(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:").append(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();
}
 
源代码20 项目: uavstack   文件: HostNewworkInfo.java
/**
 * @return {wlan0={ips={"192.168.1.109":{"bcast":"192.168.1.255","mask":"255.255.255.0"}}, mac=28:C6:3F:C4:29:23}}
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map getNetCardInfo() {

    Map netCardInfos = new HashMap<String, Map<String, String>>();
    Enumeration<NetworkInterface> netInterfaces;
    try {
        netInterfaces = NetworkInterface.getNetworkInterfaces();
        while (netInterfaces.hasMoreElements()) {
            NetworkInterface ni = netInterfaces.nextElement();
            // if (ni.isVirtual()) {
            // continue;
            // }

            Map ipInfos = new HashMap<String, Map<String, String>>();
            List<InterfaceAddress> addresses = ni.getInterfaceAddresses();
            for (InterfaceAddress ia : addresses) {
                if (ia.getAddress().isLoopbackAddress() || ia.getAddress().getHostAddress().indexOf(":") != -1) {
                    continue;
                }

                Map ipInfo = new HashMap<String, String>();

                InetAddress bcast = ia.getBroadcast();
                ipInfo.put("bcast", bcast == null ? "UNKNOWN" : bcast.getHostAddress());
                ipInfo.put("mask", NetmaskLengthToNetmask(ia.getNetworkPrefixLength()));

                ipInfos.put(ia.getAddress().getHostAddress(), ipInfo);
            }

            if (!ipInfos.isEmpty()) {
                Map netCardInfo = new HashMap<String, Map<String, String>>();
                netCardInfo.put("ips", JSONHelper.toString(ipInfos));
                netCardInfo.put("mac", getMacAddressAsString(ni));
                netCardInfos.put(ni.getName(), netCardInfo);
            }
        }
    }
    catch (SocketException e) {
        // ignore
    }

    return netCardInfos;
}
 
源代码评论
动弹
沙发等你来抢
 方法所在类