简体   繁体   中英

Android UDP packet loss : why?

I'm writing an android app which consist of udp communication with some linux machine(a very low spec machine with camera).

The machine consists of an rtsp server, and a udp server which responds to certain keywords and responses via udp packet.

The hardware developer says that there was nothing wrong when he tested it with his laptop, but losses are accurring in my android device.

Below is my source.

It will send a single command, and the device will send back two packets with length of 10 for each.

Can someone inspect if there are any faults that may cause loss in this source?

 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 is a Connection Less Protocol which doesn't guarantee Data Delivery at the Communication Layer. You need to implement a Protocol in your Application Layer to guarantee Data Delivery. SEND XXX , ACK XXX . If you don't receive an ACK you must resend the Data.

Or simply you use TCP which Connection Oriented and Guarantees Delivery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM