Java Broadcast Address Get getBroadcastAddresses()

Here you can find the source of getBroadcastAddresses()

Description

Retrives all the broadcast address of the machine (depending of the different network interfaces)

License

Open Source License

Return

an arraylist of broadcast addresses

Declaration

public static List<InetAddress> getBroadcastAddresses() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    /**/* www  .j a  v a 2 s  .  c  o m*/
     * Retrives all the broadcast address of the machine (depending of the different network interfaces)
     * @return an arraylist of broadcast addresses
     */
    public static List<InetAddress> getBroadcastAddresses() {
        List<InetAddress> addresses = new ArrayList<>();
        Enumeration<NetworkInterface> interfaces;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException ex) {
            return addresses;
        }

        for (NetworkInterface networkInterface = interfaces.nextElement(); interfaces
                .hasMoreElements(); networkInterface = interfaces
                .nextElement())
            try {
                if (networkInterface.isLoopback())
                    continue;

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

                    addresses.add(broadcast);
                }
            } catch (SocketException ex) {
                //empty
            }

        return addresses;
    }
}

Related

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