简体   繁体   English

到达EOS后重置Java Socket的InputStream

[英]Resetting the InputStream of a Java Socket after reaching EOS

I'm attempting to connect two separate applications via a TCP/IP socket. 我试图通过TCP / IP套接字连接两个单独的应用程序。 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. 要接收数据我正在使用InputStream.Read()函数,一旦它收到值-1它应该停止处理。 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. 但是,现在的问题是,如果另一组数据出现在InputStream已经在流结束( EOS ),那么所有发送的新数据都将被丢弃或忽略。 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. 我找到解决这个问题的唯一方法是,一旦达到End of Stream,我关闭套接字并重新打开它,我认为可能会以其他方式更好地处理。

How can I reset the InputStream so that it is ready for the next set of data that may come along? 如何重置InputStream以便为下一组可能出现的数据做好准备?

You are getting EOS when reading from a TCP/IP Socket because the other end has closed the write side of its socket. 从TCP / IP套接字读取时,您正在获取EOS,因为另一端已关闭其套接字的写入端。 Once this happens, the TCP/IP protocol provides no way to "unclose" the connection. 一旦发生这种情况,TCP / IP协议就无法“取消关闭”连接。

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. 如果您不想打开新连接,则需要在TCP / IP传输协议之上的应用程序协议,该协议允许您表示每个逻辑数据集的结尾...而不需要关闭。

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

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