Java Socket OutputStream is not flushing

北慕城南 提交于 2019-12-23 17:37:03

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/2208387/java-socket-outputstream-is-not-flushing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!