Java.Io.Streamcorruptedexception: Invalid Type Code: 00

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

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().

Serialization in Java, invalid type code 00

You have implemented a recursive writeObject method: when you write an instance to an output stream, it calls the writeObject method, which writes an int, and then writes the object to the output stream, which write an int, etc.

The goal of the serialVersionUID field is to check that the objects are compatible. It's done natively by the serialization mechanism. You don't have to do anything except changing the serialVersionUID value when the class changes.



Related Topics



Leave a reply



Submit