title: What's the correct and working way to broadcast an UDP packet in Java?
body: <p>I need to broadcast an UDP packet on every network interface. At first, I tried broadcasting to <code>255.255.255.255</code>, with no results, and I later discovered that this &quot;has been deprecated for about 20 years&quot;. So I tried iterating on every network interface in order to get the broadcast address of the interface and then send an UDP packet to that address.</p>
  <p>Still, the following code:</p>
  <pre><code>public static Collection&lt;InetAddress&gt; getBroadcastAddresses() {
      try {
          Collection&lt;InetAddress&gt; result = new LinkedList&lt;InetAddress&gt;();
          Enumeration&lt;NetworkInterface&gt; nets = NetworkInterface.getNetworkInterfaces();
          for (NetworkInterface netint : Collections.list(nets))
                  for (InterfaceAddress address : netint.getInterfaceAddresses()) {
                      InetAddress broadcastAddress = address.getBroadcast();
                      if (broadcastAddress != null)
                          result.add(broadcastAddress);
                  }
          return result;
      } catch (SocketException e) {
          throw new RuntimeException(e);
      }
  }
  
  public static void broadcast(int port, DatagramPacket packet,
          DatagramSocket socket, PrintWriter logger) throws IOException {
  
      packet.setPort(port);
  
      for (InetAddress address : getBroadcastAddresses()) {
          logger.println(&quot;Broadcasting to: &quot;+address);
          packet.setAddress(address);
          socket.send(packet);
      }
      
  }
  </code></pre>
  <p>prints this stuff:</p>
  <pre><code>Broadcasting to: /0.255.255.255
  Broadcasting to: /255.255.255.255
  Broadcasting to: /255.255.255.255
  Broadcasting to: /255.255.255.255
  Broadcasting to: /255.255.255.255
  </code></pre>
  <p>which is really annoying. Am I supposed to grab the IP address and netmask for every network interface and perform bitwise operations to &quot;build&quot; the correct broadcast address? <a href="https://stackoverflow.com/questions/683624/udp-broadcast-on-all-interfaces">This seems to me like Unix socket programming in C</a>... Is there a clean, Java way to neatly deliver a miserable UDP packet to all the buddies that crowd my network?</p>
  <p><strong>EDIT</strong>: searching the web, it turned out that this time my code is not broken. Instead, the JVM is. The data you get from <code>InterfaceAddress.getBroadcast()</code> is inconsistent, at least under Windows 7. See for example <a href="http://web.archiveorange.com/archive/v/9zwtbZAkSzOXBSu2TcLT" rel="nofollow noreferrer">this</a> and <a href="http://enigma2eureka.blogspot.com/2009/08/finding-your-ip-v4-broadcast-address.html" rel="nofollow noreferrer">this</a>: the solution seems to set a Java system property in order to make it prefer IPv4 over IPv6, but this doesn't work for me. Even with the suggested workaround, I get <strong>different results on every different run</strong>, and since the broadcast address I get is apparently random, I suspect I'm given data taken from undefined state memory locations.</p>