I got a server waiting for messages and a client sending him messages, when using
PrintWriter out = new PrintWriter(lpSocket.getOutputStream(), true);
String msg;
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
while ((msg = userIn.readLine()) != null)
out.print(msg + (char) 10);
nothing happens, meaning the server just keep waiting for a message that never arrive. When I change the out.print
to out.println
it works.
I would like to know why
By default, PrintWriter
calls flush
in println
, whereas it doesn't do this in print
. You can fix this by adding out.flush()
after your out.print
call
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.