What is the correct and working way to transmit a UDP packet in Java?

I need to broadcast a UDP packet on each network interface. At first I tried broadcasting to 255.255.255.255 without any results, and later I found that it was "out of date 20 years." So I tried iterating on each network interface in order to get the broadcast address of the interface, and then send a UDP packet to that address.

However, the following code:

 public static Collection<InetAddress> getBroadcastAddresses() { try { Collection<InetAddress> result = new LinkedList<InetAddress>(); Enumeration<NetworkInterface> 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); } } 

prints this material:

 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 

which is really annoying. Do I have to grab the IP address and netmask for each network interface and perform bitwise operations to “build” the correct broadcast address? It seems to me like programming Unix sockets in C ... Is there a clean Java way that neatly delivers a pathetic UDP packet to all the buddies that change my network?

EDIT : An internet search, it turned out that this time my code did not break. Instead, the JVM. The data obtained from InterfaceAddress.getBroadcast() is not compatible, at least under Windows 7. See for example this and this : the solution seems to set the Java system property to make it prefer IPv4 over IPv6, but this does not work for me . Even with the proposed workaround, I get different results every time I start , and since the broadcast address I get is apparently random, I suspect that I was given data taken from undefined memory locations. Serious, serious ...

The implementation of the Address interface is broken . Now I have a big problem on my side, because I do not know how to develop this network application. IP multicast is widely supported. I just want to translate some crap to the desired UDP broadcast address, without forcing the user to write it in the text box.

+7
source share
1 answer

You need to get the network IP address and mark it and use for broadcast. This is the easy part. Then you need to collect all the responses, knowing that some servers may not have received UDP packets, and some of the answers might be lost. You must consider the fact that UDP is designed to be unreliable.

I would parse ipconfig / all to get the IP and submask. Even ipconfig has only an IPv4 subpattern

0
source

All Articles