3

In my experiment,

if Server has this:

ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());

then client side has to do this, in the opposite order:

ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());

Otherwise server and client will deadlock.

What's the reason for this? and is there a formal API spec for it?

user207421
  • 305,947
  • 44
  • 307
  • 483
Qishan
  • 165
  • 2
  • 12
  • I would have thought attempting to read from an `InputStream` before anything is written would cause a problem (as the read would block), meaning if both the client and server attempt to read from there respective `InputStreams` in the same thread you were hoping to use writing to, then you'd run into problems... – MadProgrammer Feb 01 '13 at 03:03
  • @MadProgrammer It *does* cause a problem. That's why he's asking. But only for object streams, because construction writes and reads a header respectively. – user207421 Feb 01 '13 at 04:42
  • @EJP - Missed the "Object" in the "Stream" :P - time for some more caffine – MadProgrammer Feb 01 '13 at 04:44
  • To answer the question in your title, no, as long as they don't both try to construct the `ObjectInputStrram` first. It is safest if they both try to construct the `ObjectOutoutStrram` first. – user207421 Dec 23 '16 at 18:15

1 Answers1

5

Yes. I see how this might happen. The javadoc for the ObjectInputStream constructor says this:

"Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header."

So if both the client and the server construct their ObjectInputStream before their ObjectOutputStream, then both will block waiting for the other end to send the serialization stream header.

Note that this is happening at the object stream level, not the socket or bytestream levels. If you are doing simple byte or character or "data" I/O over a socket, you don't need to worry about the order in which the streams are constructed.

Also not, that this is not a problem if you have separate threads on (both) the client and server sides to do the reading and writing. All things being equal, that is probably a better architecture because it allows the client/server communication over the socket to be "full duplex".

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • So the requirement is to create the ObjectOutputStream first in at least one of the ends, and life being what it is the rule should be to create it first in both ends. – user207421 Feb 01 '13 at 05:25
  • @EJP - yea ... but if the client or server uses 2 threads to construct + read / construct + write, then you don't need to bother. – Stephen C Feb 01 '13 at 05:36