英文:
Why does ServerSocketChannel#bind require API level 24?
问题
我尝试在Android上(最低API级别21)以以下方式打开NIO服务器套接字通道。
ServerSocketChannel.open().also {
it.bind(InetSocketAddress(0))
}
但上述代码给我一个错误,提示需要API级别24。理论上,我可以使用ServerSocket
然后结束,但我的客户端代码在使用SocketChannel
、ByteBuffer
等,混合使用这两者感觉设计不良!
还让我困惑的另一件事是,我可以在最低API级别21上进行open()
,但不能在API级别21上进行bind()
,那它有什么作用呢?
英文:
I'm trying to open a NIO server socket channel as follows on Android (min API 21).
ServerSocketChannel.open().also {
it.bind(InetSocketAddress(0))
}
But the above code gives me an error saying that - Call requires API level 24. In theory, I can use the ServerSocket
and call it a day, but my client code is using SocketChannel
, ByteBuffer
etc. and mixing the two feels like a bad design!
Another thing that confuses me is that I can open()
it at min API level 21 but can't bind()
it at API level 21, then what purpose does it serve?
答案1
得分: 0
目前,我找到了一种替代方法来绑定服务器套接字并仍然使用ServerSocketChannel
,而无需将API级别更改为24。
ServerSocketChannel.open().also {
it.socket().bind(InetSocketAddress(0))
}
我仍然对在API级别24之后才添加ServerSocketChannel#bind
的决定感到困惑。
英文:
For now, I've found an alternate way to bind the server socket and still use the ServerSocketChannel
without changing the API level to 24.
ServerSocketChannel.open().also {
it.socket().bind(InetSocketAddress(0))
}
I'm still confused about the decision to add ServerSocketChannel#bind
as late as API level 24.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论