Java ObjectInputStream hanging

I feel really stupid right now guys .... basically I connect via TCP on the local machine ... and when I try to make In / out streams on the client, it will not receive the transfer by creating the input of the stream object. What gives? This stops after printing 2 ... no exceptions or anything else ... This is not the first time I have used this class, which is partly why I am puzzled.

try { System.out.println("1"); mySocket = new Socket("localhost", 11311); System.out.println("12"); oos = new ObjectOutputStream(mySocket.getOutputStream()); System.out.println("2"); ois = new ObjectInputStream(mySocket.getInputStream()); System.out.println("13"); } catch (Exception e) { e.printStackTrace(); } 
+6
source share
2 answers

From the specification of the ObjectInputStream object:

This constructor will block until the corresponding ObjectOutputStream wrote and discarded the header.

+6
source

(For future readers :) I had the same problem, because I made stupid changes in the server program and did not test it for a long time, then I was confused why the program was blocked.

ServerSocket accepts the connection ( responderSocket = serverSock.accept(); ), and then unexpectedly for the unattractive if program (the stupid changes I mentioned!) Jumps out of the stream, and because I did not add a finally block to close the streams and sockets , the socket was left without access or without any messages (even stream headers). Thus, in the client program there was no thread header (When I debbugged. In the code, I saw that the last function executed before the lock was:

 public ObjectInputStream(InputStream in) throws IOException { verifySubclass(); bin = new BlockDataInputStream(in); handles = new HandleTable(10); vlist = new ValidationList(); enableOverride = false; readStreamHeader(); //// <== This function bin.setBlockDataMode(true); } 

readStreamHeader(); )

Therefore, be careful what is happening on the server side, maybe the problem is not where you expect it!

+1
source

Source: https://habr.com/ru/post/898458/


All Articles