Get broadcast address in Java

Description

The following code shows how to get broadcast address.

Example


import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
//w w  w .  j  ava  2 s .  c  o  m
public class Main {
  public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
      NetworkInterface ni = en.nextElement();
      System.out.println(" Display Name = " + ni.getDisplayName());

      List<InterfaceAddress> list = ni.getInterfaceAddresses();
      Iterator<InterfaceAddress> it = list.iterator();

      while (it.hasNext()) {
        InterfaceAddress ia = it.next();
        System.out.println(" Broadcast = " + ia.getBroadcast());
      }
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode