当前位置:网站首页 > 编程语言 > 正文

udp广播报文(udp 广播)



1.AndroidManifest.xml文件添加网络权限

2.获取网卡IP地址,将末尾改成.255,作为广播发送地址

public static List<String> adds = new ArrayList<>();

public static void resetAdds() {

       adds.clear();

       try {

           Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();

           if (enumeration != null) {

               while (enumeration.hasMoreElements()) {

                   NetworkInterface wf = enumeration.nextElement();

                   Enumeration<InetAddress> ei = wf.getInetAddresses();

                   if (ei != null) {

                       while (ei.hasMoreElements()) {

                           InetAddress ia = ei.nextElement();

                           if (!ia.isLoopbackAddress()) {

                               String address = ia.getHostAddress();

                               if (address != null && address.indexOf(":") == -1) {

                                   for (int i = address.length() - 2; i >= 0; i--) {

                                       if (address.charAt(i) == '.') {

                                           adds.add(address.substring(0, i) + ".255");

                                           break;

                                       }

                                   }

                               }

                           }

                       }

                   }

               }

           }

       } catch (Exception e) {

           e.printStackTrace();

       }

   }

3.用线程发送udp广播

public static void udpSend(byte[] data) {

       new Thread() {

           @Override

           public void run() {

               DatagramSocket socket = null;

               try {

                   socket = new DatagramSocket();

                   for (int i = 0; i < UdpSendService.adds.size(); i++) {

                       DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(UdpSendService.adds.get(i)), UdpSendService.portNumber);

                       socket.send(packet);

                   }

socket.close();

               } catch (Exception e) {

e.printStackTrace();

               } finally {

                   if (socket != null) {

                       socket.close();

                   }

               }

           }

       }.start();

   }

4.用线程接收udp广播

public static void udpReceive() {

       for (int i = 0; i < adds.size(); i ++) {

           int finalI = i;

           new Thread() {

               @Override

               public void run() {

                   DatagramSocket socket = null;

                   try {

                       socket = new DatagramSocket(portNumber, InetAddress.getByName(adds.get(finalI)));

                       byte[] buffer = new byte[4096];

                       DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

                       while (true) {

                           socket.receive(packet);

                           String message = new String(packet.getData(), 0, packet.getLength());

                            Log.d("test_udp_receive", "result:" + message);

                       }

                   } catch (Exception e) {

                       e.printStackTrace();

                   } finally {

                       if (socket != null) {

                           socket.close();

                       }

                   }

               }

           }.start();

       }

   }

到此这篇udp广播报文(udp 广播)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • noneu是什么意思(none是什么意思译)2025-01-09 18:45:10
  • 接口400错误是什么原因引起的(接口500错误原因解决方法)2025-01-09 18:45:10
  • 重绘图标啥意思(重绘图标怎么用?)2025-01-09 18:45:10
  • 二级域名解析查询系统(二级域名查询工具)2025-01-09 18:45:10
  • 启动优盘如何制作(如何制作优盘启动项)2025-01-09 18:45:10
  • ht22.vip域名查询(ht60.vip域名查询)2025-01-09 18:45:10
  • 动态库(动态库存表)2025-01-09 18:45:10
  • 网易云怎么获取位置权限(网易云音乐获取位置信息怎么设置)2025-01-09 18:45:10
  • 跨域是什么烟(跨域是什么意思 怎么解决)2025-01-09 18:45:10
  • 来自远方合集txt百度云(来自远方合集txt百度云谨言百度网盘)2025-01-09 18:45:10
  • 全屏图片