At times I get a StreamCorruptedException when trying to read via an
ObjectInputStream attached to a Socket:
java.io.StreamCorruptedException: InputStream does not contain a
serialized object
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:849)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:168)
...
It's some kind of timing issue- it only occurs if the sender begins the
send *before* the receiver calls getInputStream() on the socket. The
object being sent through the stream is perfectly valid (in fact in most
cases it's nothing but an Integer).
Would appreciate any advice from someone who might have encountered this
problem before.
-Paul
Mistake above- I meant to say that the problem only occurs if the
receiver calls getInputStream() before the sender begins the send. This
makes me think that possibly getInputStream() isn't blocking as it
should and is somehow getting garbage data.
Can you show the code where you make the socket connection and create
both ends of the ObjectStream? Are you doing anything else with the
socket other than creating the ObjectStreams at both ends?
This simple example works fine for me on Linux (linux 2.2.19,
glibc2.1.2, Sun JDK 1.3.0):
server:
ServerSocket ss = new ServerSocket(43210);
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Integer foo = (Integer)ois.readObject();
ois.close();
client:
Socket s = new Socket("localhost",43210);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(new Integer(47));
oos.close();
Letting either the client or the server sleep before creating the
streams makes no difference to the results I get.
/gordon
--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
ericsson research
stockholm, sweden