The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.net  [27 examples] > Multicast  [3 examples]

e154. Receiving from a Multicast Group

Once you've created a multicast socket and joined the group, all datagrams sent to its corresponding multicast address will be available to be read from the socket. You can read from the socket just like you would from a unicast socket.
    public void read(MulticastSocket msocket, byte[] inbuf) {
        try {
            DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);
    
            // Wait for packet
            msocket.receive(packet);
    
            // Data is now in inbuf
            int numBytesReceived = packet.getLength();
        } catch (IOException e) {
        }
    }

 Related Examples
e153. Joining a Multicast Group
e155. Sending to a Multicast Group

See also: Datagram    Encodings    HTTP    Hostnames and IP Addresses    Sockets    URLs   


© 2002 Addison-Wesley.