到达EOS后重置Java Socket的InputStream
我正在尝试通过 TCP/IP 套接字连接两个单独的应用程序。在这种情况下,两个应用程序都不作为另一个应用程序的“客户端”运行,将它们描述为两个需要相互通信的独立服务器会更容易。
为了接收数据,我使用 InputStream.Read()
函数,一旦接收到 -1
值,它就应该停止处理。然而,现在的问题是,如果另一组数据出现,InputStream
已经位于流末尾 (EOS
),因此所有正在发送的新数据都会被丢弃或被忽略。我发现解决这个问题的唯一方法是,一旦到达流结束,我关闭套接字并重新打开它,我认为这可能可以通过其他方式更好地处理。
如何重置 InputStream 以便它为可能出现的下一组数据做好准备?
I'm attempting to connect two separate applications via a TCP/IP socket. In this case neither of the applications is running as a "client" of the other, it would be easier to describe them as two separate servers who need to communicate with each other.
To receive the data I'm using the InputStream.Read()
function and once it receives a value of -1
it should stop processing. However, now the problem is that if another set of data comes along the InputStream
is already at the End of Stream (EOS
) and so all new data being sent is discarded or ignored. The only way I have found to fix this problem is that once the End of Stream has been reached I close the socket and reopen it which I think might be handled better some other way.
How can I reset the InputStream so that it is ready for the next set of data that may come along?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 TCP/IP 套接字读取数据时,您会收到 EOS,因为另一端已关闭其套接字的写入端。一旦发生这种情况,TCP/IP 协议就无法提供“取消关闭”连接的方法。
如果您不想打开新连接,则需要 TCP/IP 传输协议之上的应用程序协议,该协议允许您表示每个逻辑数据集的结束……而无需关闭。
You are getting EOS when reading from a TCP/IP Socket because the other end has closed the write side of its socket. Once this happens, the TCP/IP protocol provides no way to "unclose" the connection.
If you don't want to have to open a new connection, you need an application protocol on top of the TCP/IP transport protocol that allows you to signify the end of each logical dataset ... without doing a close.