小编use*_*385的帖子

android 上如何接收广播包

我正在尝试实现我的应用程序能够接收一些广播消息。一台设备向 192.168.2.255 发送消息。我可以在 Wireshark 上看到这有效。我的应用程序应该会收到这些消息,但它还不起作用。这是我的代码:

int port = 3123;

        // Create a socket to listen on the port.
        DatagramSocket dsocket = new DatagramSocket(port);
        dsocket.setBroadcast(true);

        // Create a buffer to read datagrams into. If a
        // packet is larger than this buffer, the
        // excess will simply be discarded!
        byte[] buffer = new byte[1024];

        // Create a packet to receive data into the buffer
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        // Now loop forever, waiting to receive packets and printing them. …
Run Code Online (Sandbox Code Playgroud)

android broadcast

2
推荐指数
1
解决办法
2791
查看次数

标签 统计

android ×1

broadcast ×1