Multicast Sniffer : MulticastSocket « Network « Java Tutorial






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

public class MainClass {

  public static void main(String[] args) throws Exception {
    int port = 0;
    InetAddress group = InetAddress.getByName("127.0.0.1");

    MulticastSocket ms = new MulticastSocket(port);
    ms.joinGroup(group);

    byte[] buffer = new byte[8192];
    while (true) {
      DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
      ms.receive(dp);
      String s = new String(dp.getData());
      System.out.println(s);
    }
    // ms.leaveGroup(group);
    // ms.close();

  }

}








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