Deserialisation issue - java.io.StreamCorruptedException: invalid type code: 00

巧了我就是萌 提交于 2019-12-02 10:14:24

receiveMSG = (Message) deserialize(receivedPacket.getData());

You need to change this to

receiveMSG = (Message) deserialize(receivedPacket.getData(), receivedPacket.getOffset(), receivedPacket.getLength);

and adjust your 'deserialise()' method accordingly, to accept 'offset' and 'length' parameters, and to use them when constructing the ByteArrayInputStream.

EDIT The code you posted doesn't compile: the expression that constructs the outgoing datagram packet is incorrect. It should be

new DatagramPacket(test, test.length, datagramSocket.getInetAddress(), datagramSocket.getPort())

I have no idea what packetOverhead is supposed to be, but you don't need it.

NB You don't need to create a new Message() the line before you call readObject().

jgitter

Are you sure you have received all of the bytes for the serialized objects? A DatagramSocket uses UDP under the hood. Are you sure that all of the packets have arrived?

Also, is it really a good idea to use UDP for transferring a serialized object? This is one case where TCP would seem to be more approriate. UDP does not guarantee delivery or delivery order which could easily cause a corrupted stream.

You should also really put a private static final long serialVersionUID = 1L; on your Message class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!