Reputation: 213
I am trying to learn socket programming. I wrote client using InputStreamReader
and with BufferedReader
I read the msg sent from server.
For server if I write PrintWriter
with print
method it works and with write
method it's not, why?
And OutputStreamReader
is not at all useful as it does not have print
method and with write
, I am not getting messages in client side.
Client:
Socket c=new Socket("143.22.165.27",6000);
InputStreamReader isr=new InputStreamReader(c.getInputStream());
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
System.out.println(s);
Server
Socket sock=s.accept();
OutputStreamWriter out =
new OutputStreamWriter(sock.getOutputStream());
out.write("...........");
Upvotes: 0
Views: 1122
Reputation: 93
You must use out.flush() every time when you want to sent portion of data via out.write(msg).
Upvotes: 0
Reputation: 269667
I assume you are using the readLine()
method on the client's BufferedReader
. So my guess would be that you aren't writing any newline characters when you use the write()
methods on the server. Thus, the client never reaches the end of a line. A PrintStream
or a PrintWriter
adds the newline characters for you whenever you call a println()
method.
Of course, without any code or even a description of the problem, it's hard to say for sure.
Upvotes: 4