StreamCorruptedException when reading from an ObjectInputStream

Hello folks!
I hope there's someone out there who can help me.
I try to write a client-server-communication (via sockets). When starting the application I get the following exception:
java.io.StreamCorruptedException: Type code out of range, is -84
     at java.io.ObjectInputStream.peekCode(Unknown Source)
     at java.io.ObjectInputStream.refill(Unknown Source)
     at java.io.ObjectInputStream.readObject(Unknown Source)
     at java.io.ObjectInputStream.readObject(Unknown Source)
     at com.sap.swf.protegecontrol.mcd.communication.StateTestClient.connect(StateTestClient.java:86)
     at com.sap.swf.protegecontrol.mcd.communication.StateTestClient.run(StateTestClient.java:42)
     at java.lang.Thread.run(Unknown Source)
I tried several suggestions from several other threads, but nothing worked for me.
Here's the excerpt of my coding:
public void connect() {
while (newConnectionRequested) {
listening = true;
Socket client = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Object input = null;
try {
client = new Socket(this.server, this.port);
oos = new ObjectOutputStream(client.getOutputStream());
catch (Exception e) {
e.printStackTrace();
System.exit(1);
System.out.println("New connection requested by client");
try {
oos.writeObject(new StateRequestInfo());
oos.flush();
catch (Exception e) {
e.printStackTrace();
try {
ois = new ObjectInputStream(client.getInputStream());
catch (Exception e) {
e.printStackTrace();
while (listening) {
try {
input = ois.readObject();
catch (Exception e) {
e.printStackTrace();
System.exit(0);
if (input != null) {
Display display = Mcd.getDisplay();
if (input instanceof String) {
System.out.println((String)input);
if (input instanceof StateInfo) {
this.state = (StateInfo)input;
if (display != null) {
display.syncExec(new Runnable() {
public void run() {
Mcd.getGUIInstance().setState(state);
packagesReceived++;
if (packagesReceived == this.END_VAL) {
listening = false;
packagesReceived = 0;
try {
oos.close();
ois.close();
client.close();
catch (Exception e) {
e.printStackTrace();
System.out.println("Connection terminated by client");
toLive++;
if (toLive == this.END_APP) {
newConnectionRequested = false;
System.out.println("Client loop ended!");
Comment: The method opens a new client socket, connects to the server and sends a StateRequestInfo-object to inform the server which type of communication is preferred. The "state"-communication should work as follows: the server sends StateInfo-objects until the client closes (when received a certain amount of packages) and re-opens connection -
this runs in an endless loop in a separate threaad.
The State-Info Object is quite large, because it contains a Vector which contains multiple objects of state-information concerning another part of the application. All self-written objects are serializable.
I hope that's enough info. Thanks in advance.

The solution for me was to create a new
ObjectInputStream for each read, which will then
expect:
header | bob | header | dougThanks for the hint, but I tried this already and it didn't fix anything.
An alternative is to skip 4 bytes (the length of the
header) after each read, but that may not work well
with socket streams.This can't be the solution because the StreamCorruptedException is also thrown when receiving the first object - that's why I think skipping the header after receiving the first StateInfo wouldn't fix anything.
But thanks for your efforts!
By the way, that's an excerpt of the server-code:
public void run() {
ObjectOutputStream out = null;
long currtime;
while (sending) {
out = null;
try {
out = new ObjectOutputStream(client.getOutputStream());
catch (Exception e) {
// e.printStackTrace();
if (out == null) {
System.out.println("Connection terminated by client");
break;
currtime = System.currentTimeMillis();
if (currtime > (reftime + this.TIME_INC)) {
System.out.println("Sending data...");
try{
out.writeObject(theManagerInfo.getStateManager().getStateInfo());
out.flush();
catch(Exception e){
reftime = currtime;
try {
if(out != null){
out.close();
client.close();
catch (Exception e) {
e.printStackTrace();
Comment: the server runs in a separate thread and sends periodically StateInfo-Objects to the client that requestet the socket communication.
Having received a certain amount of StateInfos the client automatically curts the link and requests a new communication

Similar Messages

Maybe you are looking for