Reputation: 107
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?
Upvotes: 0
Views: 2048
Reputation: 8348
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:
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
).
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");
Upvotes: 3
Reputation: 35051
Yes I've absolutely seen this before. If you are using a PrintWriter or another Writer that has autoflush, you do not need to call flush(). But otherwise, to get the message to send, you've got to call flush() to get the content in the Writer / OutputStream to send.
Upvotes: 0