【问题标题】:Android UDP packet loss : why?Android UDP 丢包:为什么?
【发布时间】:2017-04-04 15:48:14
【问题描述】:

我正在编写一个 android 应用程序,其中包含与一些 linux 机器(一个非常低规格的带摄像头的机器)的 udp 通信。

该机器由一个 rtsp 服务器和一个 udp 服务器组成,该服务器通过 udp 数据包响应某些关键字和响应。

硬件开发人员说他用笔记本电脑测试时没有任何问题,但在我的安卓设备中出现了损失。

以下是我的来源。

它将发送一个命令,并且设备将返回两个数据包,每个数据包长度为 10。

有人可以检查是否有任何可能导致此来源丢失的故障吗?

 private DatagramSocket clientSocket;

public void stopUdpSocket(){
    try {
        if (clientSocket != null) {
            clientSocket.close();
        }
    }catch (Exception e){
        Log.e("test", "error", e);
    }
}

public void sendUdp(final Context context, final String command, final Handler handler) {
    new AsyncTask<Void, Void, List<String>>() {

        @Override
        protected List<String> doInBackground(Void... voids) {
            Log.e("Test", "send $SNAPSHOT onLine");
            try {
                clientSocket = new DatagramSocket(9999, InetAddress.getByName("0.0.0.0"));
                byte[] sendData = new byte[1024];
                byte[] receivedata = new byte[1024];

                String sentence = command;
                DatagramPacket receivePacket = new DatagramPacket(receivedata, receivedata.length);

                sendData = sentence.getBytes();
                String receivedData = " ";
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("192.168.5.1"), 9999);
                clientSocket.send(sendPacket);
                List<String> temp = new ArrayList<>();
                List<String> result = new ArrayList<>();

                int timeoutCounter = 0;

                do {
                    if (timeoutCounter == 4) {
                        try {
                            Log.e("Test", "UDP TIMEOUT");
                            clientSocket.close();
                        } catch (Exception e) {
                            Log.e("test", "Execption", e);
                        }
                        return null;
                    }

                    clientSocket.receive(receivePacket);
                    receivedData = new String(receivePacket.getData(), 0, receivePacket.getLength());

                    Log.e("Test", receivedData + ", IP CHECK Sender:  : " + receivePacket.getAddress().toString() + ", port : " + receivePacket.getPort());

                    if (!receivedData.equals("!HEARTBEAT")) {
                        Log.e("Test", "ADD : " + receivedData);
                        temp.add(receivedData);
                        timeoutCounter = 0;
                    }

                    if (temp.size() == 2) {
                        break;
                    }
                    receivePacket = new DatagramPacket(receivedata, receivedata.length);
                    timeoutCounter++;
                } while (true);

                clientSocket.close();
                Log.e("Test", "End of UDP TASK - client socket closed");

                for (String s : temp) {
                    result.add(s.substring(2, s.length() - 1));
               }

                return result;

            } catch (Exception e) {
                Log.e("Test", "e", e);
            }finally {
                stopUdpSocket();
            }
            return null;
        }//end of doInBackground
    }.execute();
}//end of sendUdp

【问题讨论】:

  • 我希望您知道 UDP 不是保证交付协议?
  • 是的,我知道。但是,硬件设备本身就是一个接入点,安卓与它的连接范围很近(不超过30cm)。我想知道我的代码是否有问题,因为开发硬件的人声称他在笔记本电脑上运行的基于 c++ 的测试用例没有任何损失。

标签: java android networking udp


【解决方案1】:

UDP 是一种无连接协议,它不保证通信层的数据传输。您需要在应用层中实现协议以保证数据交付。 SEND XXXACK XXX。如果您没有收到ACK,您必须重新发送数据。

或者只是使用面向连接并保证交付的 TCP。

【讨论】:

  • 好吧,我知道这不是一个可靠的协议。但是,硬件开发人员声称他在笔记本电脑上运行的基于 c++ 的测试用例没有任何损失。同一个网络接入点,笔记本和智能手机的网络容量会不会不同?
猜你喜欢
  • 2013-08-26
  • 1970-01-01
  • 2011-04-17
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 2018-03-18
  • 2013-11-14
相关资源
最近更新 更多