0

my project consists of 2 parts: server side and client side. When I start server side everything is OK, but when I start client side from time to time I get this error:

java.io.IOException: stream active
    at java.io.ObjectOutputStream.reset(Unknown Source)
    at client.side.TcpConnection.sendUpdatedVersion(TcpConnection.java:77)
    at client.side.Main.sendCharacter(Main.java:167)
    at client.side.Main.start(Main.java:121)
    at client.side.Main.main(Main.java:60)

When I tried to run this project on the other pc this error occurred even more frequently. In Java docs I found this bit.

Reset may not be called while objects are being serialized. If called inappropriately, an IOException is thrown.

And this is the function where error is thrown

void sendUpdatedVersion(CharacterControlData data) {
        try {
            ServerMessage msg = new ServerMessage(SEND_MAIN_CHARACTER);
            msg.setCharacterData(data);
            oos.writeObject(msg);
            oos.reset();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I tried to put flush() but that didn't help. Any ideas? Besides, no errors on server side.

2
  • What are you trying to achieve with the reset() call? Commented Nov 23, 2014 at 13:26
  • I know that new object is created every time so in theory i don't need it but somehow if i don't reset it sends old data. Commented Nov 23, 2014 at 13:29

3 Answers 3

1

I think you're misunderstanding what reset() does. It resets the stream to disregard any object instances previously written to it. This is pretty clearly not what you want in your case, since you're sending an object to the stream and then resetting straight away, which is pointless.

It looks like all you need is a flush(); if that's insufficient then the problem is on the receiving side.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are confusing close() with reset(). use

oos.close();

instead of oos.reset();

Comments

0

calling reset() is a perfectly valid thing to want to do. It is possible that 'data' is reused, or some field in data is reused, and the second time he calls sendUpdatedVersion, that part is not sent. So those who complain that the use is invalid are not accurate. Now as to why you are getting this error message

What the error message is saying is that you are not at the top level of your writeObject call chain. sendUpdatedVersion must be being called from an method that was called from another writeObject.

I'm assuming that some object is implementing a custom writeObject() and that method, is calling this method.

So you have to differentiate when sendUpdatedVersion is being called at the top level of the call chain and only use reset() in those cases.

2 Comments

yeah you are right. I'm using reused data. That's why I need reset(). However i'm not sure about the error. I was too stupid to close streams on the server side so maybe that was a reason.. it looks like it works fine now. i'll check on other machine later.
It is a perfectly valid thing to do unless you're in the middle of a writeObject() call, i.e. inside a private writeObject() method, which is when you get this exception, as stated in the Javadoc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.