在我的实现中,我使用Java序列化来发送一组二进制对象,这些二进制对象以通知标头开头,该通知标头告知将传递多少个对象以及它们各自的大小。刚开始进行简单测试,我就能够成功传输头对象,之后我尝试只发送一个1024字节的数据对象。但是在运行测试后,我会观察以下内容:
InvalidClassException
的截断。 有人可以提供一些指示,说明这个问题是什么以及如何解决这个问题?
接收器线程run()方法如下所示。
public void run() {
while (mRunning) {
try {
// Receive the serialized object
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println("SERVER: data received: ");
// Display the serialized object.
byte[] data = packet.getData();
System.out.println(Arrays.toString(data));
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
try {
Object obj = is.readObject();
if (obj instanceof DatakNotification) {
System.out.println("Data notification received: ");
} else if (obj instanceof DataChunk) {
System.out.println("Data Chunk received: ");
}
System.out.println(obj);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
mRunning = false;
}
}
socket.close();
}
在日志中,我看到如下。
第一个对象(标题)
SERVER: data received:
[-84, -19, 0, 5, 115, 114, 0, 47, 99, 111, 109, 46, 99, 111, 110, 97, 46, 118, 105, 100, 101, 111, 99, 108, 105, 101, 11
0, 116, 46, 99, 104, 117, 110, 107, 103, 101, 110, 46, 66, 108, 111, 99, 107, 78, 111, 116, 105, 102, 105, 99, 97, 116,
105, 111, 110, -41, 62, 52, -41, -16, 78, 119, 0, 2, 0, 5, 73, 0, 10, 109, 66, 108, 111, 99, 107, 83, 105, 122, 101, 73,
0, 10, 109, 67, 104, 117, 110, 107, 83, 105, 122, 101, 73, 0, 14, 109, 76, 97, 115, 116, 67, 104, 117, 110, 107, 83, 10
5, 122, 101, 73, 0, 12, 109, 84, 111, 116, 97, 108, 67, 104, 117, 110, 107, 115, 76, 0, 8, 109, 66, 108, 111, 99, 107, 7
3, 100, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 120, 112, 0, 0, 10
1, 48, 0, 0, 4, 0, 0, 0, 1, 48, 0, 0, 0, 26, 116, 0, 36, 97, 53, 100, 48, 51, 54, 52, 55, 45, 54, 52, 51, 52, 45, 52, 55
, 98, 100, 45, 97, 101, 55, 51, 45, 55, 102, 57, 53, 49, 48, 52, 102, 55, 49, 98, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
收到阻止通知: Block Id:a5d03647-6434-47bd-ae73-7f95104f71b8块大小:25904块大小:1024最后块大小:304总块数:26
第二个对象(1K数据)
服务器:收到的数据: [-84,-19,0,5,115,114,0,39,99,111,109,46,99,111,110,97,46,118,105,100,101,111,99,108 ,105,101,11 0,116,46,99,104,117,110,107,103,101,110,46,68,97,116,97,67,104,117,110,107,-95,-44,27, 37,-39, - 34,12,17,2,0,5,73,0,7,109,76,101,110,103,116,104,90,0,7,10,10,77,97,114,107,101, 100,73,0,12,1 09,83,101,113,117,101,110,99,101,78,117,109,91,0,4,109,66,117,102,116,0,2,91,66,76, 0,8,109,6 7,104,117,110,107,73,100,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105, 110,103 ,59,120,112,0,0,0,0,0,0,0,0,117,114,0,2,91,66,-84,-13,23,-8,6, 8,84,-32,2,0,0,120,112,0 ,0,4,0,70,76,86,1,1,0,0,0,9,0,0,0,0,18,0,0,-73,0,0,0,0, 0,0,0,2,0,10,111,110,77,101,11 6,97,68,97,116,97,8,0,0,0,8,0,100,117,114,97,116,105,111,110,0,0,0,0, 0,0,0,0,0,0,5,119 < .....> -124,-7,-22,-22,-62,86,12,1,45,29,-126,-128,24,6,-48,96,3,-53,-15,-95 ,-24,48,31,1,10,-4,24,7 9,-63,41,68,6]
java.io.InvalidClassException: com.dataxfer.chunkgen.DataChunk; local class incompatible: stream classdesc serialVersionUID = -6785768889157546991, local class serialVersionUID = 2835159210520537273
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:617)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at com.dataxfer.transceiver.LocalUdpReceiver.run(LocalUdpReceiver.java:62)
答案 0 :(得分:2)
这意味着您的作家和读者的serialVersionUID
不匹配。这是因为作者和读者的这个类的代码是不同的。
这意味着需要对读取的serialVersionUID进行硬编码以匹配编写器。
尝试设置
private static final long serialVersionUID = -6785768889157546991L;
仅当您未更改任何字段的类型或删除任何字段时,此操作才有效。