快速接收UDP数据包

时间:2014-10-18 16:46:49

标签: java android udp

我有一个UDP侦听器线程,它被循环并在收到数据包后调用一个方法。它工作得很好,只是当第一个数据包快速到达后,没有调用该方法。

两个数据包一个接一个地发送,第一个是2个字节长,第二个是3个字节长。 当第二个数据包到达时,receive()方法往往会被触发。

以下是UDP侦听器类:

public class UDPListener extends Thread{
    private boolean running = true;
    byte[] data = new byte[1500];
    DatagramPacket packet = new DatagramPacket(data, data.length);

    public void run() {
        Log.v("MR", "Runs");
        try {
            while(running) {
                Log.v("MR", "Listening... ");

                socket.receive(packet);
                Log.v("MR", "Received Data: " + packet.getData()[0] + "; Length: " + packet.getLength());

                device.post(new Runnable() {
                    public void run() {
                        device.receive(packet.getData(), packet.getData().length);                           
                    }
                });

            }
        } catch (Throwable e) {
            e.printStackTrace();
            Log.v("MR", "Failed receiving");
        }

    }
}

这是device.receive()方法:

public final void receive(byte[] inData, int length) {
    data = inData;
    dataLength = length;
    Log.v("MR", "Receive(): Data: +" + data[0] + "; Length: " + String.valueOf(data.length));
}

以下是日志结果的一部分:

10-18 17:24:34.393  Received Data: 79; Length: 3
10-18 17:24:34.393  Listening...
10-18 17:24:34.393  Receive(): Data: +79; Length: 1500
10-18 17:24:34.393  Receive(): Data: +79; Length: 1500
10-18 17:24:34.413  Received Data: 6; Length: 2
10-18 17:24:34.413  Listening...
10-18 17:24:34.413  Received Data: 79; Length: 3
10-18 17:24:34.413  Listening...
10-18 17:24:34.413  Receive(): Data: +79; Length: 1500
10-18 17:24:34.413  Receive(): Data: +79; Length: 1500
10-18 17:24:34.423  Received Data: 6; Length: 2
10-18 17:24:34.423  Listening...

由于可以看到数据包实际进入,因此对于第二个(长度:3)数据包,只会更频繁地调用receive()。

'device'是一个自定义视图,带有receive()方法。 我也尝试使用handler和runOnUiThread()与Runnable,但结果相同。 我假设UI线程无法如此快速地更新或类似的东西。

另外,为什么在线程日志中有实际长度的数据包和resume()日志中的1500字节?它应该是通过的相同数据包。

1 个答案:

答案 0 :(得分:0)

我看到这个问题有80多个观点。解决方案是使用处理程序向UI发送消息,并在新数据包到来之前重新初始化data []数组和DatagramPacket。

工作代码是:

public static class UDPListener extends Thread{
        private boolean running = true;
        Message msg;
        byte[] data;
        DatagramPacket packet;

        public void run() {
            try {
                while(running) {
                    data = new byte[1500];
                    packet = new DatagramPacket(data, data.length);

                    //Blocks and waits for a packet
                    socket.receive(packet);

                    //Send packet message to UI
                    msg = UDPReceiveHandler.obtainMessage();
                    msg.obj = packet;
                    UDPReceiveHandler.sendMessage(msg);

                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }