aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Cannot flush() using BufferedWriter or PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Cannot flush() using BufferedWriter or PrintWriter" Watch "Cannot flush() using BufferedWriter or PrintWriter" New topic
Author

Cannot flush() using BufferedWriter or PrintWriter

Anand Kapadi
Greenhorn

Joined: Jan 15, 2004
Posts: 26
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

Joined: Oct 29, 2001
Posts: 8710
    
    6

I'm not sure if this is your problem, but I sure wouldn't read a file like this:


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Anand Kapadi
Greenhorn

Joined: Jan 15, 2004
Posts: 26
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

Joined: Oct 29, 2001
Posts: 8710
    
    6

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

Joined: Mar 08, 2011
Posts: 6
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
 
wood burning stoves
 
subject: Cannot flush() using BufferedWriter or PrintWriter
 
Similar Threads
Sockets
Can we write in a file from an array???
Send a String to Server and server send it somewhere on network
Dynamic IP address and socket programming.
BufferedReader .readLine() is not seeing end of file