Im trying to send multiple serializable objects of different classes through a single
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
But, I always get a
java.io.StreamCorruptedException: invalid type code: AC
Ive been through the forum and tried out.reset() but it doesnt seem to work.
Am I doing something wrong or missing something?
Please help
It is the stream that is corrupted, not the
ObjectOutputStream. That is the name of a class.This problem arises when you try to read the result of multiple
ObjectOutputStreamswith a singleObjectInputStream. It isn’t valid.ObjectOutputStreamwrites a header which starts with – guess what? 0xAC. So when you use a singleObjectInputStreamto read a stream that has been created by multipleObjectOutputStreams, it finds an unexpected 0xAC and throws that exception.Solution: don’t do that. You can’t append multiple
ObjectOutputStreamsto a file, and you can’t use multipleObjectOutputStreamsover a socket unless you have extraordinary co-ordination sufficient to create a newObjectInputStreamat the precise point in the stream where the lastObjectOutputStreamleft off: instead, you must use the sameObjectInputStreamandObjectOutputStreamfor the life of the socket.