package 第十二章; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; import java.util.Scanner; public class GuangBoSrc { // 发送广播 public static void main(String[] args) throws IOException { InetAddress ip=InetAddress.getByName("230.0.0.1"); MulticastSocket multicastSocket=new MulticastSocket(55555); multicastSocket.joinGroup(ip); byte[]b="该起床了!!".getBytes();<pre name="code" class="java">package 第十二章; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; import java.net.UnknownHostException; public class GuangBoDes { // 接收广播 public static void main(String[] args) throws IOException { InetAddress ip=InetAddress.getByName("2300.0.1"); MulticastSocket multicastSocket=new MulticastSocket(55555); multicastSocket.joinGroup(ip); byte[]b=new byte[1024]; DatagramPacket packet =new DatagramPacket(b, b.length); multicastSocket.receive(packet); String str=new String(packet.getData(),0,packet.getLength()); } }
DatagramPacket packet=new DatagramPacket(b, b.length, ip, 55555);multicastSocket.send(packet);}}