Java Socket OutputStream 未刷新

发布于 2024-08-20 08:52:17 字数 232 浏览 8 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒儿 2024-08-27 08:52:17

啊是的,愚蠢的问题,你只需要使用 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.

青春如此纠结 2024-08-27 08:52:17

我会尝试调用此方法而不是刷新:

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

查看是否由于某种原因无法进行刷新(checkError 在内部调用刷新并记住错误状态)。

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

浏览器可能决定在显示 html 之前故意等待更多输入(特别是在 html 格式不完美的情况下)。我似乎记得自己过去也遇到过这个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文