Java Broadcast Address Get getBroadcastAddresses()

Here you can find the source of getBroadcastAddresses()

Description

get Broadcast Addresses

License

Open Source License

Declaration

public static List<InetAddress> getBroadcastAddresses() 

Method Source Code

//package com.java2s;
/*/*from   w w  w.  j av a  2  s. com*/
 * Copyright 2015-2016 The OpenDCT Authors. All Rights Reserved
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    public static List<InetAddress> getBroadcastAddresses() {
        return getBroadcastAddresses(true);
    }

    private static List<InetAddress> getBroadcastAddresses(boolean retry) {
        List<InetAddress> returnValue = new ArrayList<>();

        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
                    .getNetworkInterfaces();

            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces
                        .nextElement();

                if (!networkInterface.isLoopback()
                        && networkInterface.isUp()) {
                    List<InterfaceAddress> addresses = networkInterface
                            .getInterfaceAddresses();

                    for (InterfaceAddress interfaceAddr : addresses) {
                        InetAddress address = interfaceAddr.getAddress();

                        if (address instanceof Inet4Address) {
                            returnValue.add(interfaceAddr.getBroadcast());
                            break;
                        }
                    }
                }
            }
        } catch (Throwable e) {
            // NetworkInterface.getNetworkInterfaces() is known to on very rare occasion throw an
            // exception when returning the interfaces. If this happens, we will try one more time
            // because giving up.
            System.out
                    .println("OpenDCT - ERROR: Unable to enumerate broadcast addresses => "
                            + e.getMessage());
            e.printStackTrace(System.out);
            if (retry) {
                return getBroadcastAddresses(false);
            }
        }

        // Last effort to get at least one broadcast address. This should be a rare situation unless
        // we're on an IPv6 only network in which case we would need to completely re-write all of
        // this. This is not ideal because Java will select an interface of its choosing to
        // broadcast from and it might not be the most desirable one.
        if (returnValue.size() == 0) {
            try {
                returnValue.add(InetAddress.getByName("255.255.255.255"));
            } catch (UnknownHostException e1) {
            }
        }
        return returnValue;
    }
}

Related

  1. getBroadcastAddress()
  2. getBroadcastAddresses()
  3. getBroadcastAddresses()
  4. getBroadcastAddresses(int port)
  5. getBroadcastAddresses(int port)