Java NIO read() End Of Stream

二次信任 提交于 2020-01-02 09:28:16

问题


I'm using Java NIO in to accept, read and write in my server.

In the documentation it said that the SocketChannel.read() function will return the number of bytes read from socket, and -1 if it reach end of stream.

Now - i don't really understand what does "End Of Stream" mean? is it as same as borken pipe error ?

Does it mean that the connection is lost for both side and i should close the SocketChannel ?

I would be really great full if someone can explain more about it - and give some example of using and behaving correctly according to it..


回答1:


End Of Stream means there is nothing more to read because the other end has closed the stream.

The value is 0 is not nothing, it is a zero.

You will get an exception if the connection is lost unexpectedly. I.e. the other end had not closed the connection gracefully.



来源:https://stackoverflow.com/questions/14037721/java-nio-read-end-of-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!