Java MulticastSocket receive from multicast client

Description

Java MulticastSocket receive from multicast client

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

public class Main {
   public static void main(String[] args) {

      try {// w w w  .ja v a2  s. com
         // Create a MulticastSocket and bind it to port 8379
         MulticastSocket socket = new MulticastSocket(8379);
         // Join to multicast group
         socket.joinGroup(InetAddress.getByName("224.0.0.1"));
         // Construct a DatagramPacket to receive packet
         byte[] in = new byte[256];
         DatagramPacket packet = new DatagramPacket(in, in.length);
         System.out.println("Waiting to receive a packet...");
         // Receive the packet now and display
         socket.receive(packet);
         String msg = new String(in, 0, packet.getLength());
         System.out.println("Received : " + msg);
      } catch (Exception ioe) {
         System.out.println(ioe);
      }
   }
}



PreviousNext

Related