0

I'm writing an client/server application which transfers mostly JPEG images wrapped in own defined Objects. Now I've got a problem with ObjectStreams over socket connection. This is not a duplicate question because I nearly read every question about this topic on StackOverflow and none of them solved my problem. What I do basically is read and write serialized Objects which contain primitive datatypes.

The error I get:

java.io.StreamCorruptedException: invalid type code: 00

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1379)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)

The error varies relating to the invalid type code (00,AC,...) and very rarely invalid stream header.

I don't want to post any code for now because I need an abstract, more theoretic answer on how a stream can get corrupted and how I could prevent it. But if really needed, I'll post it of course. I use the default readObject() and writeObject() methods for the transfer of only 1 type of Object which is defined for server and client in the same file. I've got only 1 instance of ObjectInputStream and OutputStream over the lifetime of the socket. ClassCastException, ClassNotFoundException and EOFException occur (don't know why too) but are handled properly.

The crazy thing about it is that the error occurs erratically and only after a few minutes proper functionality.

That is why I need to know which situations else can "corrupt" a stream. As I said it works perfect for a couple of minutes (in this space of time are thousands and more Objects transferred properly) and then somewhen it appears.

I appreciate any participation and thoughts on this topic. Thank You

1 Answers1

0

I suggest you examine the stack of streams on the server side first. (then the client side stack, but I presume it is much simpler).

If you had a simple Objectoutputstream over some bufferedoutputstream over a socketoutputstream, this would likely not happen.

Your server might be having strange filter streams, like a gzip, or some tomcat/jboss wrapper, perhaps httpservletresponsewrapper's outputstream interfering. Could be i/o errors that corrupt your stream, like a connection closed but the error is masked and misrepresented by the streams stack. Wireshark is your friend for ruling out obvious nw issues.

user2023577
  • 1,752
  • 1
  • 12
  • 23