~/ learn/ comp-348/ cards/ Multicast: one send, many receivers
1 of 6

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

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/4241929a-50da-469e-94ec-45deec116123/flashcard utf-8 LF