简体   繁体   中英

Not reading whole socket input stream in some cases

I have a java application which communicates according to the tcp length protoocol. So if header includes and invalid length value then it reject reading further input stream and writes an error message to outputstream of the socket and closes the socket. Is this a problem in networking perspective or should I consume the garbage input stream till the end of file anyway ?

In this case the remote host will get SocketException or alike. This can happen before it has read the error message you sent to TCP stream; it may event not finish with writing to a socket. Generally, there is no way to know if the remove peer has finished reading from its socket unless a) it sends something you may recognize as read acknowledgement or b) you read the whole input message till EOF then send response and close connection gracefully.

And this won't cause any garbage problems, OS will release all unneeded buffers.

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