清单 1. 简单服务器 public class Server { public static void main(String [] arstring) { try { // Create a multicast datagram socket for receiving IP // multicast packets. Join the multicast group at // 230.0.0.1, port 7777. MulticastSocket multicastSocket = new MulticastSocket(7777); InetAddress inetAddress = InetAddress.getByName("230.0.0.1"); multicastSocket.joinGroup(inetAddress); // Loop forever and receive messages from clients. Print // the received messages. while (true) { byte [] arb = new byte [100]; DatagramPacket datagramPacket = new DatagramPacket(arb, arb.length); multicastSocket.receive(datagramPacket); System.out.println(new String(arb)); } } catch (Exception exception) { exception.printStackTrace(); } } }
清单 2. 简单客户机 public class Client { public static void main(String [] arstring) { try { // Create a datagram package and send it to the multicast // group at 230.0.0.1, port 7777. byte [] arb = new byte [] {'h','e','l','l','o'}; InetAddress inetAddress = InetAddress.getByName("230.0.0.1"); Da