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

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -