Java examples for Network:UDP
A UDP Multicast Socket That Receives UDP Multicast Messages
import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; public class Main { public static void main(String[] args) throws Exception { InetAddress mcIPAddress = InetAddress.getByName("127.0.0.1"); MulticastSocket mcSocket = new MulticastSocket(5555); System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress()); mcSocket.joinGroup(mcIPAddress);// ww w . j a v a 2 s . c o m DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); while (true) { System.out.println("Waiting for a multicast message..."); mcSocket.receive(packet); String msg = new String(packet.getData(), packet.getOffset(), packet.getLength()); System.out.println("[Multicast Receiver] Received:" + msg); } // mcSocket.leaveGroup(mcIPAddress); // mcSocket.close(); } }