Question

Situation : A client communicates to a server via a single socket connection. The Client can randomly send serialized objects, or just simple parameters, the the server.

Problem : How to make the server simultaneously listen for ObjectInputStream and InputStream from the Client ?

What I'm thinking of: A 'master' thread spawns 2 child Threads. One child listens to ObjectInputStreams from the client, and the other child listens to InputStreams.

Will this approach work ? Because I've heard ObjectInputStream is blocking. What about thread safety ? Synchronization issues ?

Was it helpful?

Solution

ObjectInputStream has also methods like readFully(byte[] buf), so there is no need to use InputStream.

Then, if you open an ObjectInputStream on an InputStream, you may not use that InputStream any more, in order not to break functionality of ObjectInputStream.

APPENDED

ObjectInputStream is a subclass of InputStream, so you can call all methods of InputStream on an ObjectInputStream instance, but not simultaneously. So you have to develop a protocol which prescribes when connection is used as ObjectInputStream and when as InputStream. If one side sends message via InputStream interface and the other reads it from ObjectInputStream (or vice versa), an error is inevitable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top