alanwilliamson

java.io.PrintWriter not responding to flush()

Discovered a real subtle problem with our MailCatcher open source project last night.  MailCatcher  is an implementation of the Apache Mailet API, and one of the many things it does is to manage spools and outgoing emails to various hosts.  Everything worked perfectly on the Windows development machine (jdk1.5 / WindowsXP) but upon deploying to production, (jdk1.5 / Linux) all outgoing email would hang.

When transporting email via SMTP, after you send the main mail body, you terminate the message with a single <dot><cr>.  This was sent, but a response was never received from the remote mail server to say it had successfully received the mail body.

The code was pretty standard:
  PrintWriter out = new PrintWriter( inSocket.getOutputStream(), true );
...
out.println( "." );
out.flush();
At first I thought it was a problem with the remote mail server, but the same thing happenned no matter which host I sent email to.  I then ran up another instance of MailCatcher in debug mode to test the outgoing aspect.  That way I could see the incoming traffic on a different machine.  Sure enough, the remote mail server never received the <dot>.  Very bizarre. Creating the PrintWriter class with auto-flush on never made any difference either.

Finally I ditched the PrintWriter class and moved to BufferedWriter and the whole thing instantly burst into life.  I had a look around to see if anyone had experienced this, and could only find a couple of things with people talking about flushing within a Servlet, but this was more to do with the way the Servlet API works as oppose to a problem with the PrintWriter class itself.

 

Recent Cloud posts

Recent JAVA posts

Latest CFML posts


 
Site Links