1

I'm trying to transfer strings from my server to my client and I'm trying to find an explanation why when I'm using PrintWriter in my server the client receives the string while when I'm using BufferedWriter the client doesn't receive the string.

In my client I have the next readers/writers:

out=new PrintWriter(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(s.getInputStream()));

In my main I'm receiving data from server with the next call:

String sol=in.readLine();

In my server I'm sending data with the next call (os is an outputStream that I get in my function):

PrintWriter out= new PrintWriter(os);
out.write("test");
out.flush();

While when I use BufferedWriter it doesn't send data to the client (or the client can't receive it?) "

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os));
out.append("test"); // tried also using out.write
out.flush();  
user207421
  • 305,947
  • 44
  • 307
  • 483
JeyJ
  • 3,582
  • 4
  • 35
  • 83
  • 1
    For the buffered writer you must use write method as well as for print writer – midugh May 25 '19 at 21:07
  • Tried the writer also, same result – JeyJ May 25 '19 at 21:09
  • 3
    Hard to tell from your code snippet, but your reader is expecting a "line", so I assume your print writer might be adding a carriage return to the data being sent, whereas the buffer is not. However, that cannot be asserted from your code. Worth a try though! – Edwin Dalorzo May 25 '19 at 21:12
  • Because you used `PrintWriter.println()`, which sends a line terminator, but `BufferedWriter.write()`, which doesn't, and you are using `readLine()`, which blocks until the line terminator is received, or end-of-stream or an exception occurs. – user207421 Mar 30 '21 at 22:52

1 Answers1

1

On my server side Bufferwriter doesn't add "\n" to the end of the string while in my client side I'm trying to read a line with inputstream. Printwriter adds "\n" in the method println. Thanks to @EdwinDalorzo for the help.

halfer
  • 19,824
  • 17
  • 99
  • 186
JeyJ
  • 3,582
  • 4
  • 35
  • 83