Join a group and read what arrives
Join a group and read what arrives
Answer
InetAddress group = InetAddress.getByName("239.1.2.3"); try (MulticastSocket socket = new MulticastSocket(4446)) { socket.joinGroup(group); DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); socket.receive(packet); System.out.println(packet.getAddress()); socket.leaveGroup(group); }
The port in the constructor must match the senders; the group address selects the group and the port still selects the application. getAddress() reports the sender unicast address, not the group, which is how one socket tells many senders apart.
Harold 4e ch13 §Multicasting; §Working with Multicast Sockets