Applet <--> Socket ObjectOutputStream and ObjectInputStream
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Stuart_Stephen
Posted On:   Wednesday, May 15, 2002 01:44 AM

Hi, I am running JDK 1.3.1 and have written a little program which recieves java objects over a network. The server and applet both send objects to each other. However, sometimes I get an error which is: java.io.StreamCorruptedException: Header out of range -84 at java/io/ObjectInputStream.readHeader at java/io/ObjectInputStream.readObject at ChatApplet/Listener.run My problem is that this seems to be quite random. I can however recreate it when another connection goes onto the server. All my objects have been serialized. I'm only sending String's at the moment for testing which are serialized according to the java documentation.    More>>

Hi,


I am running JDK 1.3.1 and have written a little program which recieves java objects over a network. The server and applet both send objects to each other. However, sometimes I get an error which is:


			
java.io.StreamCorruptedException: Header out of range -84
at java/io/ObjectInputStream.readHeader
at java/io/ObjectInputStream.readObject
at ChatApplet/Listener.run


My problem is that this seems to be quite random. I can however recreate it when another connection goes onto the server.


All my objects have been serialized. I'm only sending String's at the moment for testing which are serialized according to the java documentation.


My reading code is:


			
reader = new ObjectInputStream(socket.getInputStream());

...

Object readObj = null;
while((readObj = (Object)reader.readObject())!=null) {
try {
if(readObj instanceof String) {
String strReadMessage = (String)readObj;
interpretMessage(strReadMessage);
if(strReadMessage.equalsIgnoreCase(" ")) {
break;
}
}
else {
// simple error logging
base.log(LogStatic.ERROR, "Invalid object recieved.");
base.log(LogStatic.ERROR, readObj.toString());
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}


My object writing code is as follows:


			
ObjectOutputStream w = null;
try {
if(sock!=null) {
if(sock.equals(socket)) {
write(msg);
}
else {
base.log(LogStatic.SENT, msg);
w = new ObjectOutputStream(socket.getOutputStream());
w.writeObject(msg);
w.flush();
w.reset(); // empty buffer incase flush() fails to
}
}
else {
write("Socket not found, message not sent.");
}
}
catch(Exception e) {
e.printStackTrace();
}


Could this be to do with different versions of the Java VM's used?


Any help would be gratefully recieved.


Thanks,

Stuart Stephen.

   <<Less
About | Sitemap | Contact