Search code examples
javanetwork-programmingobjectinputstream

new ObjectInputStream blocks despite flushing OutputStream


At the moment i am trying to make some experiences with networking but i can't get past some point in the connection establishing process while using ObjectIn/OutputStreams.

I read in several Threads (like this one: Java Creating a new ObjectInputStream Blocks )on problems about the constructor of ObjectInputStream blocking and many suggest flushing the output first, so i came up with this:

out = new ObjectOutputStream(socket.getOutputStream());
String test = "test"; 
out.writeObject(test);
out.flush();
InputStream is = socket.getInputStream();
in = new ObjectInputStream(is);

The blocking line is the last one. Any suggestions why it is still failing to complete? Any help is appreciated.


Solution

  • The line of code you indicate will block until the peer has created an ObjectOutputStream on its socket, or written something else to the socket (which will cause a StreamCorruptedException).