1
如何檢測java中的udp數據包損壞?java udp檢測數據包損壞
public class PacketReceiver implements Runnable{
byte[] dataReceive = new byte[udpConnectionManager.MAX_PACKET_SIZE];
private ArrayList<Thread> workerList = new ArrayList<Thread>();
@Override
public void run() {
while(true){
DatagramPacket receivePacket = new DatagramPacket(dataReceive, dataReceive.length);
try {
udpConnectionManager.socket.receive(receivePacket);
} catch (IOException e) {
e.printStackTrace();
}
byte[] receivedData = receivePacket.getData();
//[0] stores basic command
//[1~4] int stores protocol id
//[5~9] int data increase counter for detect packet loss
//[10~14]
switch(receivedData[0]){
//initial packet
case 0x01:
if(!udpConnectionManager.instance.isInitialized(receivePacket)){
Thread t = new Thread(new AcceptThread(receivePacket));
t.start();
workerList.add(t);
}else{
System.out.println("initialized packet attempt to initialize.");
}
//heartbeat signal
case 0x02:
if(udpConnectionManager.instance.isInitialized(receivePacket)){
udpConnectionManager.instance.getConnection(receivePacket).onHeartBeat();
}else{
System.out.println("Received HeartBeat signal from non-initialized connection");
}
//
case 0x03:
}
}
}
}
數據包損壞可能會發生。我該如何處理使用udp的數據包損壞問題? 和我知道如何檢測數據包丟失,但我不知道如何檢測數據包損壞。