UDP广播发送端:
1 public static void main(String[] args) throws IOException { 2 DatagramSocket socket = new DatagramSocket(); 3 String s = "Hello 广播"; 4 byte[] bytes = s.getBytes(); 5 InetAddress address = InetAddress.getByName("255.255.255.255");//广播地址,只要是在同一个局域网之内的都会接收到 6 DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 1025); 7 socket.send(packet); 8 socket.close(); 9 10 11 }
接收端和单播接收端完全相同不做讲解