Java Socket OutputStream 未刷新
我正在用java编写一个基于套接字的服务器。客户端连接到它(网络浏览器),服务器发回一个简单的 html 代码并设置 cookie 以在客户端下次再次连接时进行识别。我正在使用 PrintStream 写入套接字,但刷新不起作用。我可以刷新的唯一方法是使用 shutdownoutput 或 close 并关闭套接字流。但我不希望这样,因为我在代码的几个地方多次读/写它。能做什么?无法从标签获得任何帮助。 我还尝试了其他缓冲编写器类,同样的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊是的,愚蠢的问题,你只需要使用 pstream.writeln("");因为 breader.readLine() 会读取直到找到 '\n' 字符。
所以 write() 不起作用。
Ah yeah , sillyproblem , you just have to use pstream.writeln(""); since breader.readLine() reads until it finds '\n' character.
so write() won't work.
我会尝试调用此方法而不是刷新:
查看是否由于某种原因无法进行刷新(checkError 在内部调用刷新并记住错误状态)。
另外值得尝试的是使用 telnet 连接到服务器并查看数据是否立即返回,或者编写一个简单的 java 客户端套接字程序来检查(从网上剪切/粘贴一个)。
浏览器可能决定在显示 html 之前故意等待更多输入(特别是在 html 格式不完美的情况下)。我似乎记得自己过去也遇到过这个问题。
I would try calling this method instead of flush:
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.