|
| 1 | +package com.baeldung.java.networking.udp.broadcast; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.DatagramPacket; |
| 5 | +import java.net.DatagramSocket; |
| 6 | +import java.net.InetAddress; |
| 7 | +import java.net.NetworkInterface; |
| 8 | +import java.net.SocketException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Enumeration; |
| 11 | +import java.util.List; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +public class BroadcastingClient { |
| 15 | + private DatagramSocket socket; |
| 16 | + private InetAddress address; |
| 17 | + private int expectedServerCount; |
| 18 | + private byte[] buf; |
| 19 | + |
| 20 | + public BroadcastingClient(int expectedServerCount) throws Exception { |
| 21 | + this.expectedServerCount = expectedServerCount; |
| 22 | + this.address = InetAddress.getByName("255.255.255.255"); |
| 23 | + } |
| 24 | + |
| 25 | + public int discoverServers(String msg) throws IOException { |
| 26 | + initializeSocketForBroadcasting(); |
| 27 | + copyMessageOnBuffer(msg); |
| 28 | + |
| 29 | + // When we want to broadcast not just to local network, call listAllBroadcastAddresses() and execute broadcastPacket for each value. |
| 30 | + broadcastPacket(address); |
| 31 | + |
| 32 | + return receivePackets(); |
| 33 | + } |
| 34 | + |
| 35 | + List<InetAddress> listAllBroadcastAddresses() throws SocketException { |
| 36 | + List<InetAddress> broadcastList = new ArrayList<>(); |
| 37 | + Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| 38 | + while (interfaces.hasMoreElements()) { |
| 39 | + NetworkInterface networkInterface = interfaces.nextElement(); |
| 40 | + |
| 41 | + if (networkInterface.isLoopback() || !networkInterface.isUp()) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + broadcastList.addAll(networkInterface.getInterfaceAddresses() |
| 46 | + .stream() |
| 47 | + .filter(address -> address.getBroadcast() != null) |
| 48 | + .map(address -> address.getBroadcast()) |
| 49 | + .collect(Collectors.toList())); |
| 50 | + } |
| 51 | + return broadcastList; |
| 52 | + } |
| 53 | + |
| 54 | + private void initializeSocketForBroadcasting() throws SocketException { |
| 55 | + socket = new DatagramSocket(); |
| 56 | + socket.setBroadcast(true); |
| 57 | + } |
| 58 | + |
| 59 | + private void copyMessageOnBuffer(String msg) { |
| 60 | + buf = msg.getBytes(); |
| 61 | + } |
| 62 | + |
| 63 | + private void broadcastPacket(InetAddress address) throws IOException { |
| 64 | + DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445); |
| 65 | + socket.send(packet); |
| 66 | + } |
| 67 | + |
| 68 | + private int receivePackets() throws IOException { |
| 69 | + int serversDiscovered = 0; |
| 70 | + while (serversDiscovered != expectedServerCount) { |
| 71 | + receivePacket(); |
| 72 | + serversDiscovered++; |
| 73 | + } |
| 74 | + return serversDiscovered; |
| 75 | + } |
| 76 | + |
| 77 | + private void receivePacket() throws IOException { |
| 78 | + DatagramPacket packet = new DatagramPacket(buf, buf.length); |
| 79 | + socket.receive(packet); |
| 80 | + } |
| 81 | + |
| 82 | + public void close() { |
| 83 | + socket.close(); |
| 84 | + } |
| 85 | +} |
0 commit comments