我正在寻找一种方法来避免 StacklessClosedChannelException,但我没有找到任何东西。我想这不安全,也不应该发生,如何避免呢?
为了避免此类异常,您应该检查通道是否处于活动状态或至少是打开的。有一个布尔验证可以实现这一点。这是 Kotlin 版本:
\n val f: ChannelFuture = bootstrap.connect(HOST, PORT).sync()\n val channel = f.channel()\n\n if (channel?.isActive == true) {\n val f: ChannelFuture? = channel?.writeAndFlush(msg)?.sync()\n else {\n //Channel not even open and therefore it\'s not writable\n }\nRun Code Online (Sandbox Code Playgroud)\n还有一个\xc3\xacsWritable布尔值,但我没有对其进行充分的测试。
| 归档时间: |
|
| 查看次数: |
9936 次 |
| 最近记录: |