Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the correct and working way to broadcast an UDP packet in Java?
    primarykey
    data
    text
    <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 "has been deprecated for about 20 years". 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("Broadcasting to: "+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 "build" 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. Serious, serious...</p> <p><strong>InterfaceAddress implementation is broken</strong>. Now I'm having a big problem on my side, because I don't know how to develop this network application. IP multicast is widely unsupported. I just want to broadcast some crap to the right UDP broadcast address without having the user write it on a text field.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload