简体   繁体   中英

Resetting the InputStream of a Java Socket after reaching EOS

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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