UDP广播

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     }

接收端和单播接收端完全相同不做讲解

 

posted @ 2020-08-18 10:33  Joker-0927  阅读(116)  评论(0编辑  收藏  举报
// 侧边栏目录