在使用PrintWriter的情况下,为什么我应该在循环中冲洗而不是在循环之后冲洗? [英] In case of PrintWriter why should I flush in the loop and not after the loop?

查看:155
本文介绍了在使用PrintWriter的情况下,为什么我应该在循环中冲洗而不是在循环之后冲洗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小演示程序中有一个服务器和客户端,我在其中将一些字符串数据从客户端发送到服务器,然后为客户端重新发送此数据,并将其写到控制台.我对PrtintWriter的flush方法感到困惑,该方法-根据JAVA文档,-刷新流.经过一些研究后,我熟悉了自动刷新的概念:当autoFlash参数为true时,println,printf或format方法将刷新输出缓冲区.我在这里唯一不了解的是为什么我应该在循环中而不是在循环之后使用PrintWriter的flush方法. (在我的情况下,我在服务器端使用PrintWriter.)因为flash.println()方法也处于循环中,所以自动闪存的功能相同.当我在循环后使用冲洗时,我的字符串数据未出现在控制台上.感谢您的指导和事先帮助!

I have a Server and Client in my little demo program, where I send some string data from the Client to the Server, and after that resending this data for the Client, which also writes it out to the console. I was confused by PrtintWriter's flush method, which is - according to the JAVA documentation, - flushes the stream. After some researching I'm getting familiar with the concept of autoflash: when the autoFlash parameter is true println, printf, or format methods will flush the output buffer. The only thing what I don't understand here is why should I use the PrintWriter's flush method in the loop and not after the loop. (In my case I use PrintWriter in the Server side.) Autoflash does the same because the println() method is in the loop too. When I use flush after the loop my string data does not appear on the console. Thank you for your guidance and help in advance!

客户:

public class ClientDemo {
public static void main(String[] args) throws IOException {

    final String CLIENTNAME = "<CLIENT>:";
    final String SERVERADDRESS = "localhost";
    final int PORT = 12312;

    try {
        Socket socket = new Socket(SERVERADDRESS, PORT);
        PrintWriter out =
          new PrintWriter(socket.getOutputStream(), true);                   
        Scanner scanner = new Scanner(socket.getInputStream());

        System.out.println(CLIENTNAME + "Client starts");
        List<String> lista = getList();

        for(String userInput : lista){
            out.println(userInput);
            System.out.println("echo: " + scanner.nextLine());
        }

    } catch(IOException e) {
        System.out.println(CLIENTNAME + "Error connecting to the server:" + e.getMessage());
    }
}

  private static List<String> getList(){
        List<String> result = new ArrayList<>();
        result.add("egy");
        result.add("ketto");
        result.add("harom");
        result.add("negy");

        result.add("ot");
        result.add("hat");
        result.add("het");
        result.add("nyolc");
        result.add("kilenc");
        result.add("tiz");

        return result;
    }
}

服务器:

public class ServerDemo {
public static void main(String args[]) throws IOException {

    final int PORT = 12312;
    final String SERVERNAME ="<SERVER>:";

    try {
        ServerSocket serverSocket = new ServerSocket(PORT);
        Socket socket = serverSocket.accept();
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
        Scanner scanner = new Scanner(socket.getInputStream());

        System.out.println(SERVERNAME + "Server starts...");

        String inputLine;
        while(scanner.hasNext()) {
            inputLine = scanner.nextLine();
            printWriter.println(inputLine);
            printWriter.flush();
        }

    } catch (IOException e) {
        System.out.println(SERVERNAME + "Error handleing client...");
    } 
   }
}

推荐答案

编写每一行后,您不必调用flush.这样做是在阻止I/O.通过调用flush,您可以确保写入套接字的每一行都是实际写入的,而不仅仅是缓冲的(稍后再写入).缓冲可以提高I/O性能.但是在这里,由于某些原因,您似乎没有利用缓冲所提供的优势.您将阻塞直到写入完全完成.

You don't have to call flush after writing every line. You are blocking the I/O by doing that. By calling flush you are ensuring that every line you are writing to the socket is actually written and not just buffered (to be written later). Buffering improves the I/O performance. But here it seems that for some reasons, you are not leveraging the advantages the buffering gives. You are blocking until the write is completely done.

这篇关于在使用PrintWriter的情况下,为什么我应该在循环中冲洗而不是在循环之后冲洗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆