开发者

Java Socket OutputStream is not flushing

开发者 https://www.devze.com 2022-12-19 13:03 出处:网络
I am writing a socket-based server in java. A client connects to it(a web-brows开发者_运维百科er) and the server sends back a simple html code and sets cookie to recognize next time client connects to

I am writing a socket-based server in java. A client connects to it(a web-brows开发者_运维百科er) 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.


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


I would try calling 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

See if it is failing for some reason to do the flush (checkError calls flush internally and remembers error state).

Also worth trying is connecting to the server using telnet and seeing if the data is being returning immediately, or writing a simple java client socket program to check (cut/paste one off the net).

It might be that the browser has decided to deliberately wait for more input before displaying your html (especially if the html is not perfectly formed). I seem to remember having this issue myself in the past.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号