Java Selector returns SelectionKey with OP_READ without data in infinity loop after writing to channel

点点圈 提交于 2019-12-03 16:45:55

read() returns -1 at EOS, which you are completely ignoring. When you get EOS, you must either close the channel or at least deregister interest in OP_READ. Otherwise you will just get another OP_READ and another -1 when you read, as you are doing, forever. Contrary to your comments above, read() returns zero on an empty read. You can ignore that, indeed you won't even see it if you only read when isReadable(), unless you read in a loop, but you must not ignore EOS.

read() returns -1 when it has read EOF. Definition:

read() returns: The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

This means you should unregister the interest for OP_READ.

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