Receiving from a Multicast Group : MulticastSocket « Network « Java Tutorial






import java.net.DatagramPacket;
import java.net.MulticastSocket;

public class Main {
  public static void main(String[] argv) throws Exception {

    MulticastSocket msocket = new MulticastSocket(9999);
    byte[] inbuf = new byte[1024];
    DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

    msocket.receive(packet);

    // Data is now in inbuf
    int numBytesReceived = packet.getLength();
  }
}








19.22.MulticastSocket
19.22.1.Multicast Sender
19.22.2.Multicast Sniffer
19.22.3.Joining a Multicast Group
19.22.4.Receiving from a Multicast Group
19.22.5.Sending to a Multicast Group