Java InetAddress Create getBroadcast(InetAddress address)

Here you can find the source of getBroadcast(InetAddress address)

Description

Gets the broadcast address for the network interface identified by the given address.

License

Open Source License

Declaration

public static InetAddress getBroadcast(InetAddress address) 

Method Source Code

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

import java.net.*;

public class Main {
    /**/*from  w  w  w. j  ava 2  s .  c o m*/
     * Gets the broadcast address for the network interface identified by the given
     * address. This will only work if the given address is a local address. It will
     * return {@code null} if it can't find an interface with the given inet address.
     */
    public static InetAddress getBroadcast(InetAddress address) {
        NetworkInterface inter = getInterface(address);
        if (inter != null) {
            for (InterfaceAddress iaddr : inter.getInterfaceAddresses()) {
                if (iaddr.getAddress().equals(address)) {
                    return iaddr.getBroadcast();
                }
            }
        }
        return null;
    }

    /**
     * Return the Network Interface object that has the given InetAddress configured
     * on it. Returns {@code null} if there are any errors locating the interface.
     */
    public static NetworkInterface getInterface(InetAddress addr) {
        try {
            return NetworkInterface.getByInetAddress(addr);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Return the Network Interface object identified with the given name. Returns
     * {@code null} if there are any errors locating the interface.
     */
    public static NetworkInterface getInterface(String name) {
        try {
            return NetworkInterface.getByName(name);
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getAddressPriority(InetAddress addr)
  2. getAddrInfo(InetAddress pAddr)
  3. getAllInetAddress(final String interfaceName)
  4. getAllIPv4InetAddresses()
  5. getAllLocalIPv4InetAddresses()
  6. getBytes(InetAddress addr, int port)
  7. getClassPart(InetAddress ip, int partNumber)
  8. getConnectionAddress(InetAddress addr)
  9. getDestinationInetAddress(URI uri)