Java Socket OutputStream is not flushing

Farah picture Farah · Feb 5, 2010 · Viewed 9.5k times · Source

I am writing a socket-based server in java. A client connects to it(a web-browser) and the server sends back a simple html code and sets cookie to recognize next time client connects to it again. I am using PrintStream to write to the socket , but flush is not working. The only way i can flush is to use shutdownoutput or close and both close the socket stream. But i do not want that because i am readin/writing to it several times in several places in the code. What can do? Could't get any help from the tags. I also tried other buffered writer classes, same problem.

Answer

Elias picture Elias · Feb 25, 2010

Ah yeah , sillyproblem , you just have to use pstream.writeln(""); since breader.readLine() reads until it finds '\n' character. so write() won't work.