Hello =) I ask you to explain to me the reason for the occurrence of such a problem. Here is a small client-server application code.

Customer:

Socket socket = new Socket("localhost", 2001); ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream())); System.out.println("step1"); ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); System.out.println("step2"); in.close(); out.close(); 

Server:

  ServerSocket ss = new ServerSocket(2001); Socket socket = ss.accept(); ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream())); System.out.println("step1"); ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); System.out.println("step2"); in.close(); out.close(); 

At startup, both consoles display only step1 and "hang".

If you do not use a buffer, i.e. don't "wrap" the received streams from the socket into the Buffered..Stream, then everything works as expected.

What is my problem? :)

    1 answer 1

    ObjectOutputStream at creation registers title, and ObjectInputStream at creation respectively, reads it. When buffering is used, these headers are stuck in buffers. To push them through, you need to set out.flush () after out = new ObjectOutputStream ().

    • Interesting, although not quite understood. But there is a solution. Thank! - horcrux