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 ?
There are several problems here.
As Peter Lawrey has pointed out, calling reset() immediately you have constructed the ObjectOutputStream is completely pointless, and probably illegal. Remove it.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With