Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot flush() using BufferedWriter or PrintWriter

 
Anand Kapadi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Checkout this code:-

server side
-----------
method 1
BufferedWriter bf=new BufferedWriter(new OutputStreamWriter(m_objClientSocket.getOutputStream(),1);
bf.write("SHUTDOWN");
bf.newLine();

method 2
PrintWriter pw=new PrintWriter(m_objClientSocket.getOutputStream(),true);
pw.println("SHUTDOWN");

setting autoflush=true;
or manually flushing using pw.flush();
----------------

handler code
-----------
public void run(){
BufferedReader br=new BufferedReader(new InputStreamReader(m_objSocket.getInputStream()));
String l_strMessage="";
while(true)
{
if((l_strMessage=br.readLine())!=null)
{
if(l_strMessage.equalsIgnoreCase("SHUTDOWN"))
{
System.out.println("breaking out.");
break;
}
}
}
}

I am getting a flushing problem.
I am spawning a threaded handler from the server side where I pass the socket reference to handle the communication.

On the server side, while shutting down I tried the two methods I pasted above to close client sockets(handler) thread .But the problem persists.
I am unable to receive the "SHUTDOWN" string at the client end using either of the two methods above.
Kindly advice.
Regards,
Anand Kapadi
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this is your problem, but I sure wouldn't read a file like this:
 
Anand Kapadi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,
No, thats not my problem.
I am unable to push messages from the server side to the client side sockets using either of these two methods.
Thanks & Regards,
Anand Kapadi
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize. I thought you had an infinite loop in your server code. Please Use Code Tags in the future to preserve your code formatting.
As for your problem, I know BufferedWriter will need to be flushed after writing data to it. PrintWriter should work the way you have it coded (autoflush = true and using println()). If you don't see the results you expect, I recommend adding a bunch of logging to make sure your code is doing what you think it is doing. The java.util.logging API makes this easy to manage.
 
Andrei Miclaus
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ess wrote:... I know BufferedWriter will need to be flushed after writing data to it. ...

You need to add a '\n' to the message for it to be sent.



This works I had the same problem just 5 min ago
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic