Java Socket OutputStream does not flush

I am writing a socket based server in java. The client connects to it (web browser), and the server sends back a simple html code and sets a cookie to recognize the next client, which connects to it again. I use PrintStream to write to the socket, but the flash does not work. The only way I can flash is to use shutdownoutput or close, and also close the socket stream. But I do not want this because I read / wrote to him several times in several places in the code. What can be done? Could get help from tags. I also tried other classes of buffered writers, same problem.

+3
source share
2 answers

Oh yes, silly question, you just need to use pstream.writeln (""); since breader.readLine () reads until it finds the \ n character. therefore write () will not work.

+4
source

I would try using this method instead of flush:

public boolean checkError()
Returns:
    True if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError method has been invoked

Look, for some reason the flash does not work (checkError causes a flash inside and remembers the error state).

It is also worth trying to connect to the server using telnet and see if the data is returned immediately, or write a simple socket program for the java client to check (cut / paste one from the network).

Perhaps the browser decided to deliberately wait for the input of additional data before displaying html (especially if html is not fully formed). I seem to remember how this problem was in the past.

0
source

All Articles