objectinputstream - Why does order of instantiation seem to matter for input and output streams in Java? -
i have following code works (please assume hostname , port initialized proper values, , message serializable class):
//example 1 - works expected message message = new message(); try(socket serversocket = new socket(hostname, port)) { objectoutputstream outstream = new objectoutputstream(serversocket.getoutputstream()); outstream.writeobject(message); outstream.flush(); objectinputstream instream = new objectinputstream(serversocket.getinputstream()); object response = instream.readobject(); } when move instantiation of objectinputstream occur after objectoutputstream instantiation, execution of application hangs indefinitely:
//example 2 - client locks message message = new message(); try(socket serversocket = new socket(hostname, port)) { objectoutputstream outstream = new objectoutputstream(serversocket.getoutputstream()); objectinputstream instream = new objectinputstream(serversocket.getinputstream()); outstream.writeobject(message); outstream.flush(); object response = instream.readobject(); } i'm looking explanation why second example locks consistently, , first example seems work without hitch. strangely, if use debugger (eclipse debugger) on client , server second example, i'm seeing message make through server, writeobject() call being executed. however, in client, debugger gets stuck on constructor objectinputstream.
if go , have read of api docs objectinputstream constructor
the important part:
this constructor block until corresponding objectoutputstream has written , flushed header.
Comments
Post a Comment