我正在编写一个Android应用程序,其中包含与某些linux机器的udp通信(带摄像头的极低规格机器)。
该机器由一个rtsp服务器和一个udp服务器组成,该服务器通过udp数据包响应某些关键字和响应。
硬件开发人员说用他的笔记本电脑测试时没有任何问题,但我的Android设备会出现损失。
以下是我的来源。
它将发送一个命令,设备将发回两个长度为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
答案 0 :(得分:0)
UDP是一种无连接协议,不保证通信层的数据传输。您需要在应用层中实现协议以保证数据传输。 getShowableDays(weeks: Array): Array {
let showableDays: Array<any> = [];
let shownMonths: Array<number> = [];
weeks.forEach((week) => {
for(let day of week.days) {
if(shownMonths.indexOf(day.getMonth()) === -1) {
showableDays.push(day);
shownMonths.push(day.getMonth());
}
}
});
return showableDays;
}
checkMonth(day): Boolean {
let showableDays: Array<any> = this.getShowableDays(this.weeks);
for(let showableDay of showableDays) {
if(showableDay === day)
return true;
}
return false;
}
,SEND XXX
。如果您没有收到ACK XXX
,则必须重新发送数据。
或者只是使用TCP面向连接和保证交付。