Search code examples
javanetwork-programmingprintwriter

Print Writer write() and println()


I am currently learning Java Network programming. In one of my Programs I just have an EchoServer which sends the message of the client. But I recognized in the client that the Printwriter.write() method just sends when I close the writer while the .println() method works fine. I also tried it with and without auto-flush.

Works:

public static void main(String[] args) {
    System.out.println("Simple Echo Client");

    try{
        System.out.println("Waiting for Connection ...");
        InetAddress localAdress = InetAddress.getLocalHost();

        try(Socket clientSocket = new Socket(localAdress,6000);
                PrintWriter out =new PrintWriter(clientSocket.getOutputStream(),true);
                BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))){

            System.out.println("Connected to Server");
            Scanner scanner = new Scanner(System.in);
            while(true){
                System.out.print("Enter text: ");
                String inputLine = scanner.nextLine();
                if("quit".equals(inputLine)){
                    break;
                }
                out.println(inputLine);
                String response = br.readLine();
                System.out.println("Server response" + response);
            }
        }

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

Doesn't work:

public static void main(String[] args) {
    System.out.println("Simple Echo Client");

    try{
        System.out.println("Waiting for Connection ...");
        InetAddress localAdress = InetAddress.getLocalHost();

        try(Socket clientSocket = new Socket(localAdress,6000);
                PrintWriter out =new PrintWriter(clientSocket.getOutputStream(),true);
                BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))){

            System.out.println("Connected to Server");
            Scanner scanner = new Scanner(System.in);
            while(true){
                System.out.print("Enter text: ");
                String inputLine = scanner.nextLine();
                if("quit".equals(inputLine)){
                    break;
                }
                out.write(inputLine);
                String response = br.readLine();
                System.out.println("Server response" + response);
            }
        }

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

Could somebody explain to me why this is the case?


Solution

  • the Printwriter.write() method just sends when I close the writer while the .println() method works fine

    There seems there may be two problems here, the first having to do with writing and the second with reading:

    1. The code creates a PrintWriter with automatic line flushing. When you use println, the new line results in the writer flushing. When using write without a new line, the PrintWriter does not flush (you can call out.flush after out.write to force a flush of the Writer).

    2. Presuming the receiving end is using Scanner.readLine(), it expects a new line or will wait until it receives one. println automatically appends the new line to the end of the String, with write you must explicitly send the new line out.write(line + "\n");