2016-11-21 1 views
0

일부 리눅스 머신 (카메라가있는 매우 낮은 사양의 컴퓨터)과 udp 통신으로 구성된 안드로이드 앱을 쓰고 있습니다.Android 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 
+1

UDP가 보장 배달 프로토콜이 아니라는 것을 알고 계셨습니까? – EJP

+0

예 알아요. 하지만 하드웨어 장치 자체가 액세스 포인트 역할을하고 안드로이드는 매우 가까운 거리 (30cm 이내)에 연결되어 있습니다. 하드웨어를 개발 한 사람이 랩톱에서 실행되는 C++ 기반 테스트 케이스에 손실이 없다고 내 코드가 잘못 되었기 때문에 궁금합니다. – March3April4

답변

0

UDP는 통신 계층에서 데이터 전달을 보장하지 않는 연결없는 프로토콜입니다. 데이터 전송을 보장하려면 응용 프로그램 계층에 프로토콜을 구현해야합니다. SEND XXX, ACK XXX. ACK을받지 못하면 데이터를 다시 보내야합니다.

또는 단순히 연결 지향 및 전달 보장 TCP를 사용하십시오.

+0

글쎄, 나는 그것이 신뢰할 수있는 프로토콜이 아니라는 것을 안다. 그러나 하드웨어 개발자는 랩탑에서 실행되는 그의 C++ 기반 테스트 케이스에 손실이 없다고 주장한다. 동일한 네트워크 액세스 지점에서 랩톱과 스마트 폰 간의 네트워크 용량이 다를 수 있습니까? – March3April4