BufferedReader readLine() blocks

前端 未结 7 2128
轻奢々
轻奢々 2020-12-06 02:02

When receiving data using readLine(), even though I put a \"\\n\" at the end of the message using the .flush when sending the message, the while loop that reads my message s

7条回答
  •  既然无缘
    2020-12-06 02:54

    This is because of the condition in the while-loop: while((line = br.readLine()) != null)

    you read a line on every iteration and leve the loop if readLine returns null.

    readLine returns only null, if eof is reached (= socked is closed) and returns a String if a '\n' is read.

    if you want to exit the loop on readLine, you can omit the whole while-loop und just do:

    line = br.readLine()

提交回复
热议问题