BufferedReader readLine() blocks

前端 未结 7 2068
轻奢々
轻奢々 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:59

    This happens because the InputStream is not ready to be red, so it blocks on in.readLine() . Please try this :

    boolean exitCondition= false;
    
    while(!exitCondition){
        if(in.ready()){
            if((line=in.readLine())!=null){
                // do whatever you like with the line
            }
        }
    }
    

    Of course you have to control the exitCondition .

    An other option can be the use of nio package, which allows asynchronised (not blocking) reading but it depend on your need.

    0 讨论(0)
提交回复
热议问题