My problem is when it tries to read the object the second time, it throws the exception:

java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at Client.run(BaseStaInstance.java:313)

java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at Client.run(BaseStaInstance.java:313)

The first time I send the exact same object message; however, when I try doing the same thing the second time, it throws the error above. Do I need to re-intialize the readObject() method? I even printed out the message object that is being received by the line below and its exact the same as the first instance where it works ok.

Object buf = myInput.readObject();

I'm assuming there's some problem with appending, but I really have no use for appending. I just want to read a fresh line everytime. I'd really appreciate some help in fixing this bug. Thank you.

==================================

Before that one line, I'm just creating the input and output objects for the socket in the run() method. The object declaration is outside the run() method in the class:-

@Override
public void run() {
    try {
        sleep((int) 1 * 8000);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        //Creating input and output streams to transfer messages to the server
        myOutput = new ObjectOutputStream(skt.getOutputStream());
        myInput = new ObjectInputStream(skt.getInputStream());
        while (true) {
            buf = myInput.readObject();
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

You're right; I don't close the object. I'm not sure how to do that.

upvote
  flag
I'm not sure where to close the object – leba-lev
upvote
  flag
NB You should catch EOFException and not treat it as an error: don't log or print the stack trace; just let it terminate the reading loop. I don't know why you're catching UnknownHostException where you are: it can't be thrown from within the corresponding try block. The sleep() is just literally a waste of time. Remove it. – EJP

2 Answers 11

The underlying problem is that you are using a new ObjectOutputStream to write to an existing ObjectInputStream that you have already used a prior ObjectOutputStream to write to. These streams have headers which are written and read by the respective constructors, so if you create another ObjectOutputStream you will write a new header, which starts with - guess what? - 0xAC, and the existing ObjectInputStream isn't expecting another header at this point so it barfs.

In the Java Forums thread cited by @trashgod, I should have left out the part about 'anew for each object at both ends': that's just wasteful. Use a single OOS and OIS for the life of the socket, and don't use any other streams on the socket.

If you want to forget what you've written, use ObjectOutputStream.reset().

And don't use any other streams or Readers or Writers on the same socket. The object stream APIs can handle all Java primitive datatypes and all Serializable classes.

1 upvote
  flag
+1 @rookie: This is the correct answer, and from the original source! :-) – trashgod
upvote
  flag
@EJP evrytime i run the code a new OOS and OIS is created. how do i manage that? – Akhil K Nambiar
upvote
  flag
@AkhilKNambiar I don't know what you're asking me that isn't already answered above. – EJP
upvote
  flag
@EJP here in my class am using two different methods for output and input streams and near input stream am getting the error. Can u tell me where to add the above code? – AndroidOptimist
upvote
  flag
@AndroidOptimist What code? I haven't posted any code. The essence of this answer is to remove code. – EJP

Faced a similar exception while using Java 7/8, i solved it by adding the below VM argument

-Dsun.lang.ClassLoader.allowArraySyntax=true

Not the answer you're looking for? Browse other questions tagged or ask your own question.