Java DatagramSocket send to multicast group
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Main { public static void main(String[] args) { try {// w w w . j a va 2 s . c o m // Create a DatagramSocket DatagramSocket socket = new DatagramSocket(); // Fill the buffer with data String msg = "Hello!"; byte[] out = msg.getBytes(); // Muticast group where packet has to sent InetAddress group = InetAddress.getByName("224.0.0.1"); // Port the receiver listens on int port = 8379; // Create a DatagramPacket with buffer, address and port DatagramPacket packet = new DatagramPacket(out, out.length, group, port); // Send to multicast IP address and port System.out.println("Sending a packet..."); // Send the packet now socket.send(packet); System.out.println("Sent : " + msg); } catch (Exception e) { e.printStackTrace(); } } }