簡體   English   中英

套接字OutputStream(BufferedWriter)不刷新味精

[英]Socket OutputStream(BufferedWriter) not flushing msg

使用一個簡單的客戶端,我試圖通過SocketOutputStream發送控制台輸入:

try (Socket socket = new Socket(host, port);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {

    Thread input = new Thread(() -> {
        String msg;
        try {
            while ((msg = in.readLine()) != null) {
                System.out.println(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    input.start();

    String msg;
    try {
        while ((msg = stdIn.readLine()) != null) {
            out.write(msg);
            out.flush(); // not flushing?
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

} catch (IOException e) {
    e.printStackTrace();
}

在發送END OF INPUT之后,似乎沒有刷新流END OF INPUT在此之后,我在服務器端收到了所有以前的嘗試,作為一個長String終止了客戶端。

使用PrintWriter而不是BufferedWriter產生相同的結果。

為什么直到從std in讀取了END OF INPUT (^ Z)數據才進入低谷?

您正在閱讀台詞,但您沒有在寫台詞。 編寫時,在消息中添加行終止符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM