1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.net.*; import java.io.*; public class MulticastS{ public static void main(String args[]) { try { int port = 2020; //224.0.0.1 ... 224.0.0.255 InetAddress multicastGroupIP = InetAddress.getByName("224.0.0.5"); System.out.println(multicastGroupIP); MulticastSocket socket = new MulticastSocket(port); socket.joinGroup(multicastGroupIP); byte[] m = "Hi".getBytes(); DatagramPacket send = new DatagramPacket(m, m.length, multicastGroupIP, port); socket.send(send); byte[] bufer = new byte[1000]; String linea; while (true) { DatagramPacket receive = new DatagramPacket(bufer, bufer.length); socket.receive(receive); linea = new String(receive.getData(), 0, receive.getLength()); System.out.println("Receive: " + linea); //Break if "Bye" if (linea.equals("Bye")) { socket.close(); break; } } socket.leaveGroup(multicastGroupIP); } catch (SocketException e) {} catch (IOException e) {} } } |
Capture 1
Capture 2
Capture 3
Capture 4