0 Replies Latest reply: Nov 13, 2012 8:52 AM by 973981 RSS

    InterfaceAddress getBroadcast() method not returning valid broadcast

    973981
      I'm having a problem broadcasting through the wireless interface on my laptop.

      The ethernet interface is working just fine, but when I use the wireless interface I keep getting different ip addresses returned for the broadcast address. Mostly it returns /0.0.0.0
      but sometimes it gives a non-zero address such as 119.0.115.0 which appears to be China, definitely not my home network, which is 192.168.2.1 and I'm not sure what the -1 means after the IP output for which I was expecting a 24



      The Ethernet output looks good
      Processing Network Interface : name:eth3 (Realtek PCIe GBE Family Controller)
      Processing Address : /192.168.2.99/24 [192.168.2.255]
      Broadcast Address : /192.168.2.255

      The wireless output looks bad
      Processing Network Interface : name:net4 (Intel(R) Centrino(R) Advanced-N 6235)
      Processing Address : /192.168.2.73/-1 [119.0.115.0]
      Broadcast Address : /119.0.115.0

      OR MOST of the time
      Processing Network Interface : name:net4 (Intel(R) Centrino(R) Advanced-N 6235)
      Processing Address : /192.168.2.73/-1 [0.0.0.0]
      Broadcast Address : /0.0.0.0


      I did find this old bug report that sounds like the same problem except it says it was resolved in java version "1.7.0_02", but I'm using "1.7.0_07" and still have the problem.


      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7158636



      This is the code I ran to generate the output above

      import java.net.InetAddress;
      import java.util.ArrayList;

      public class Broadcast {
      public static ArrayList<InetAddress> getBroadcastAddresses() {
      ArrayList<InetAddress> broadcastAddressList = new ArrayList<>();
      ArrayList<java.net.NetworkInterface> networkInterfaceList = new ArrayList<>();
      try {
      java.util.Enumeration<java.net.NetworkInterface> netInterfaceEnum = java.net.NetworkInterface.getNetworkInterfaces();

      while (netInterfaceEnum.hasMoreElements()) {
      java.net.NetworkInterface networkInterface = netInterfaceEnum.nextElement();

      if (!networkInterface.isLoopback()) {
      networkInterfaceList.add(networkInterface);
      }
      }

      // get all broadcast addresses from all interfaces
      for (java.net.NetworkInterface netInterface : networkInterfaceList) {
      System.out.println("Processing Network Interface : " + netInterface);
      java.util.List<java.net.InterfaceAddress> ifaddrList = netInterface.getInterfaceAddresses();

      for (java.net.InterfaceAddress interfaceAddr : ifaddrList) {
      System.out.println(" Processing Address : " + interfaceAddr);
      InetAddress broadcastAddress = interfaceAddr.getBroadcast();
      if (broadcastAddress == null) {
      System.out.println(" Address has no Broadcast Address");
      } else {
      System.out.println(" Broadcast Address : " + broadcastAddress);

      if (!broadcastAddressList.contains(broadcastAddress)) {
      broadcastAddressList.add(broadcastAddress);
      } else {
      System.out.println(" Broadcast Address is Already in List");
      }
      }
      }
      }
      } catch (java.net.SocketException e) {
      System.out.println("Socket Error - " + e);
      }
      return broadcastAddressList;
      }

      public static void main(String[] args) {
      Broadcast.getBroadcastAddresses();
      }
      }