4

Before I post this I looked at some past questions on this exceptions but couldn't find an exact answar.

I have a client server app which is basically a socket program connects with TCP. I got this Exceptions from client side after it runs fine for some time. But still, the client is sending data to the server even though it throws Exceptions.( may be as Event objects are passed continuously). But the server works fine as it receives the data. The Exception I get from the client side while sending data is

java.io.IOException: stream active .. This occurs from the "LINE 01" as mentioned in the code below.

Here is the client code I used.

        // And "Event" objects are passed continuously to this method one by one.              

         SocketChannel socketChannel = null;

        try {

        socketChannel = SocketChannel.open(new InetSocketAddress(host, port));
        oos = new ObjectOutputStream(socketChannel.socket().getOutputStream());
        oos.reset(); -----------> LINE 01
        oos.writeObject(event); 

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

Here is the server code

    ServerSocketChannel serverSocketChannel = null;
    try {
        serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(port));

        SocketChannel socket = serverSocketChannel.accept();
        ObjectInputStream ois = new ObjectInputStream(socket.socket().getInputStream());
         do {
                  Object object = ois.readObject();
                  if(object instanceof Event) {
                      Event event = (Event)object ;
                      viewDetailsInUI(event);
                  }

           } while (true);

Here is the stack trace I got from the client side.

java.io.IOException: stream active
    at java.io.ObjectOutputStream.reset(ObjectOutputStream.java:478)
    at org.demo.siddhi.server.EventSenderClient.sendEventToSubscriber(EventSenderClient.java:42)
    at org.demo.siddhi.server.query.types.SimpleStockQuoteVWAPQueryProvider$3.callBack(SimpleStockQuoteVWAPQueryProvider.java:344)
    at org.siddhi.core.OutputStreamHandler.run(OutputStreamHandler.java:61)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

Can anyone please explain why is this ?

3 Answers 3

4

There are several problems here.

  1. As Peter Lawrey has pointed out, calling reset() immediately you have constructed the ObjectOutputStream is completely pointless, and probably illegal. Remove it.

  2. You are using SocketChannels in blocking mode via streams, i.e. you are just using the underlying Sockets in both cases. You would be much better off using a Socket and a ServerSocket. It's a lot simpler and clearer.

  3. Your server loops reading an ObjectInputStream for multiple objects, but your client creates a new connection, sends one object, and then (I hope) closes it. These do not add up. Either your client should conserve the TCP connection and the ObjectOutputStream and use it to write multiple objects, in which case you may need to call reset() after writeObject(), and the server needs to break out of the loop when it gets EOFException, or your server can close its connection after reading one object, and the while (true) loop is unnecessary.

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

2 Comments

Thanks. As the code is bit coupled, difficult to change from NIO channels impl. I add the reset() after the write() but same result. And now I have added a producer consumer model using a blocking queue and works fine some times. But now I am getting java.io.StreamCorruptedException: invalid type code:00 . I will ask this in a new thread as this is a new topic
Subash Chaturanga No it isn't. Your code doesn't work. I don't think you've even posted it all. Fix the code as suggested above and continue the conversation here, don't hop about all over the palce.
3

It appears to believe it is serializing an object already.

IOException if reset() is invoked while serializing an object.

You don't need to call reset() at the start as there is nothing to reset(). I would drop it and it may work fine.

If you want to call reset regularly, you can call it after writeObject().

You should also call flush() somewhere as the stream is buffered.

6 Comments

reset() is called because, when sending data, old data may sent if i didn't call reset(). Isn't it ????
Its a new connection, there is no old data.
OK, thanks. I tried by removing reset() call, but still same result, and additionally in the server side, i'am getting an java.io.StreamCorruptedException: invalid type code:00 and couple of more values of the code such as FF, and etc. Can't imagine why this Exception occurred..Any idea ??
I have never seen NIO Channels used this way. Can you try using regular ServerSocket and Socket instead?
@Peter Lawrey I use them that way when testing certain things, and it's ridiculous, but that change won't solve the problem.
|
1

Look to OOS code:

 493 if (depth != 0) {
 494               throw new IOException("stream active");
 495           }

2 Comments

Hi, thanks for the quick response, what is "depth" here ?? you mean to add this code in the catch block ???
Nope, i mean OOS throw IOException then "if reset() is invoked while serializing an object" (from javadoc). I think it's immposible in your sample, because your newer call write* methods, but i try to find usage "depth" varible in OOS class - it change only if the stream is writing something.

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.