• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot flush() using BufferedWriter or PrintWriter

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Old but gold they say...
Just had the same issue! Thanks for posting this, after adding the new line, it works as expected!
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I'm pretty sure that the flush() call does send the data. But the receiving app might not know to flush its output text without that line-termination.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic