BufferedReader readLine() blocks

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

    if you want to get what's in the socket without being forced to close it simply use ObjectInputStream and ObjectOutputStream ..

    Example:

    ObjectInputStream ois;
    ObjectOutputStream oos;
    
    ois = new ObjectInputStream(connection.getInputStream());
    
    String dataIn = ois.readUTF(); //or dataIn = (String)ois.readObject();
    
    oos = new ObjectOutputStream(connection.getOutputStream());
    oos.writeUtf("some message"); //or use oos.writeObject("some message");
    oos.flush();
    
    .....
    

提交回复
热议问题