Here you can find the source of getBroadcastAddresses(int port)
Parameter | Description |
---|---|
port | port to be added to get socket address. |
public static InetSocketAddress[] getBroadcastAddresses(int port)
//package com.java2s; /*/*w w w. j a v a 2 s . co m*/ * Copyright (c) 2004 by Cosylab * * The full license specifying the redistribution, modification, usage and other * rights and obligations is included with the distribution of this project in * the file "LICENSE-CAJ". If the license is not included visit Cosylab web site, * <http://www.cosylab.com>. * * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, * OR REDISTRIBUTION OF THIS SOFTWARE. */ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { /** * Get broadcast addresses. * @param port port to be added to get socket address. * @return array of broadcast addresses with given port. */ public static InetSocketAddress[] getBroadcastAddresses(int port) { Enumeration<NetworkInterface> nets; try { nets = NetworkInterface.getNetworkInterfaces(); } catch (SocketException se) { // fallback return new InetSocketAddress[] { new InetSocketAddress("255.255.255.255", port) }; } ArrayList<InetSocketAddress> list = new ArrayList<InetSocketAddress>(10); while (nets.hasMoreElements()) { NetworkInterface net = nets.nextElement(); try { if (net.isUp()) { List<InterfaceAddress> interfaceAddresses = net.getInterfaceAddresses(); if (interfaceAddresses != null) for (InterfaceAddress addr : interfaceAddresses) if (addr.getBroadcast() != null) { InetSocketAddress isa = new InetSocketAddress(addr.getBroadcast(), port); if (!list.contains(isa)) list.add(isa); } } } catch (Throwable th) { // some methods throw exceptions, some return null (and they shouldn't) // noop, skip that interface } } InetSocketAddress[] retVal = new InetSocketAddress[list.size()]; list.toArray(retVal); return retVal; } /** * Get a set of broadcast addresses. * @return set of broadcast addresses. */ public static Set<InetAddress> getBroadcastAddresses() { Set<InetAddress> set = new HashSet<InetAddress>(10); Enumeration<NetworkInterface> nets; try { nets = NetworkInterface.getNetworkInterfaces(); } catch (SocketException se) { // fallback try { set.add(InetAddress.getByAddress(new byte[] { (byte) 255, (byte) 255, (byte) 255, (byte) 255 })); } catch (UnknownHostException e) { // noop } return set; } while (nets.hasMoreElements()) { NetworkInterface net = nets.nextElement(); try { if (net.isUp()) { List<InterfaceAddress> interfaceAddresses = net.getInterfaceAddresses(); if (interfaceAddresses != null) for (InterfaceAddress addr : interfaceAddresses) { InetAddress ba = addr.getBroadcast(); if (ba != null) // Set class takes care of duplicates set.add(ba); } } } catch (Throwable th) { // some methods throw exceptions, some return null (and they shouldn't) // noop, skip that interface } } return set; } }