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
How to Round Time to the Nearest Quarter Hour in Java
Is There a Priorityqueue Implementation with Fixed Capacity and Custom Comparator
Why Is Files.Lines (And Similar Streams) Not Automatically Closed
How to Show Changes Between Commits with Jgit
Java List Sorting: How to Keep a List Permantly Sorted Automatically Like Treemap
Best Way to Create a Hashmap of Arraylist
What Is the Purpose of Accesstype.Field, Accesstype.Property and @Access
Creating Runnable Jar with External Files Included
Jaxb :Need Namespace Prefix to All the Elements
Differencebetween Synchronized on Lockobject and Using This as the Lock
How to Listen for Key Presses (Within Java Swing) Across All Components
Are Thread.Sleep(0) and Thread.Yield() Statements Equivalent
How to Programmatically Inject a Java Cdi Managed Bean into a Local Variable in a (Static) Method
Java 8: How to Work with Exception Throwing Methods in Streams
Apache Spark - Foreach VS Foreachpartition When to Use What
Ordering of Elements in Java Hashset
How Can "This" of the Outer Class Be Accessed from an Inner Class