I have a small program whereby in the main thread, I ask for input from the user in the console.
System.out.print("Alternatively, enter peer's ID to connect:");
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader bReader = new BufferedReader(reader);
String peerID = bReader.readLine();
and in a separate thread I listen from my sockets' InputStream. If I receive something from this stream, i then try to "unblock" the readLine by calling System.in.close()
without waiting for the user's input. The main thread can then proceed to do something with the information obtained either from the socket's read or from the user.
Somehow it seem to work on my Mac, but if I try it on Windows, stepping through the debugger, I've found that System.in.close()
blocks and the whole program will hangs.
Any idea why and how should i unblock readline()
? Otherwise what would be a good way of rewriting the logic?