简体   繁体   English

Java Socket OutputStream没有刷新

[英]Java Socket OutputStream is not flushing

I am writing a socket-based server in java. 我在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. 客户端连接到它(Web浏览器),服务器发回一个简单的html代码并设置cookie以识别下次客户端再次连接它。 I am using PrintStream to write to the socket , but flush is not working. 我使用PrintStream写入套接字,但刷新不起作用。 The only way i can flush is to use shutdownoutput or close and both close the socket stream. 我可以刷新的唯一方法是使用shutdownoutput或close并关闭套接字流。 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(""); 啊,是的,愚蠢的问题,你只需要使用pstream.writeln(“”); since breader.readLine() reads until it finds '\\n' character. 因为breader.readLine()读取,直到找到'\\ n'字符。 so write() won't work. 所以write()不起作用。

I would try calling this method instead of flush: 我会尝试调用此方法而不是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). 查看是否由于某种原因导致刷新失败(checkError在内部调用flush并记住错误状态)。

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). 另外值得尝试的是使用telnet连接到服务器并查看数据是否正在立即返回,或者编写一个简单的Java客户端套接字程序来检查(从网上剪切/粘贴一个)。

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). 可能是浏览器决定在显示你的html之前故意等待更多输入(特别是如果html没有完美形成)。 I seem to remember having this issue myself in the past. 我似乎记得过去曾经有过这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM