Sending to Socket

F

FilexBB

I compose a client program that capture System.in and send it to
socket as following, it work for server that can receive what client
sent to

1: Socket s = new Socket("localhost", 12345);
2: BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
3: PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
4: //BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()));
5: String str = br.readLine();
6: pw.println(str);
7: //bw.write(str);
8: //bw.flush();


But if comment line 3 and 6 and uncomment line 4, 7 and 8, it doesn't
work on server that can't recieve anything. My question is why it work
on PrintWriter with set to flush at constructor level, but not work
for BufferedWriter method ".flush()". Hope to clarify it.

Regards,
FilexBB
 
T

Tony Morris

FilexBB said:
I compose a client program that capture System.in and send it to
socket as following, it work for server that can receive what client
sent to

1: Socket s = new Socket("localhost", 12345);
2: BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
3: PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
4: //BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()));
5: String str = br.readLine();
6: pw.println(str);
7: //bw.write(str);
8: //bw.flush();


But if comment line 3 and 6 and uncomment line 4, 7 and 8, it doesn't
work on server that can't recieve anything. My question is why it work
on PrintWriter with set to flush at constructor level, but not work
for BufferedWriter method ".flush()". Hope to clarify it.

Regards,
FilexBB

Despite your call to flush(), the BufferedReader may still hold a buffer.
I forget why and where I read it.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top