What is the correct and effective way to broadcast UDP packets in Java?
I need to broadcast UDP packets on each network interface At first, I tried to play to 255.255 255.255, no results. Later, I found that this "has been abandoned for about 20 years" Therefore, I try to iterate over each network interface to get the broadcast address of the interface, and then send UDP packets to that address
Still, 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); } }
Print this:
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
It's really annoying Should I get the IP address and netmask for each network interface and perform bitwise operations to "build" the correct broadcast address? This sees to me like UNIX socket programming in C... Is there a clean Java way to skillfully deliver poor UDP packets to all friends who gather on my network?
Editor: I searched the Internet and found that my code was not damaged this time Instead, the JVM is From interfaceaddress The data obtained by getbroadcast () is inconsistent, at least under Windows 7 See, for example, this and this: the solution seems to set java system properties to make it prefer IPv4 over IPv6, but it doesn't work for me Even with the proposed solution, I get different results in different runs, and because the broadcast address I get is obviously random, I doubt I can get data from a undefined state memory location Serious, serious
The interfaceaddress implementation was interrupted Now I have a big problem because I don't know how to develop this network application IP multicast is widely not supported I just want to broadcast some garbage to the correct UDP broadcast address without users writing it on the text field
Solution
You need to get the network IP and tag it and use it for broadcasting The simple part Then, you need to collect all replies because you know that some servers may not have received UDP packets, and some replies may have been lost You must consider the fact that UDP is designed to be unreliable
I will directly parse ipconfig / all to get IP and sub mask Even ipconfig has only a sub mask of IPv4