/** * Creates an unmodifiable local IP address set. * * @return an unmodifiable local IP address set. */ private static Set<InetAddress> createLocalInetAddresses() { final Set<InetAddress> result = new HashSet<InetAddress>(11); try { final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { final NetworkInterface networkInterface = enumeration.nextElement(); final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { result.add(inetAddresses.nextElement()); } } } catch (final SocketException ignored) { ExceptionUtils.ignoreException(ignored, "Couldn't obtain local addresses"); } return Collections.unmodifiableSet(result); }
/** * Creates an array of sockets with TTL and network interface set. * * @param mcastTTL multicast TTL. * @return an array of multicast sockets to broadcast on. * @throws IOException if I/O error occurred while creating a multicast socket. * @noinspection SocketOpenedButNotSafelyClosed, ConstantConditions */ private static MulticastSocket[] createSockets(final int mcastTTL) throws IOException { Exception lastException = null; // Records last error in case we could not create any sockets final List<MulticastSocket> socketList = new ArrayList<MulticastSocket>(11); final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { try { final NetworkInterface netIf = enumeration.nextElement(); final MulticastSocket socket = new MulticastSocket(); // NOPMD socket.setTimeToLive(mcastTTL); socket.setNetworkInterface(netIf); socket.setSendBufferSize(SEND_BUFFER_SIZE); socketList.add(socket); } catch (final Exception e) { lastException = e; ExceptionUtils.ignoreException(e, "continue to connect to those we can"); } } if (socketList.isEmpty()) { throw new IOException( "Could not create at least one multicast socket. Last error: " + lastException); } return socketList.toArray(new MulticastSocket[socketList.size()]); }