为什么 ServerSocketChannel#bind 需要 API 等级 24?

huangapple go评论79阅读模式
英文:

Why does ServerSocketChannel#bind require API level 24?

问题

我尝试在Android上(最低API级别21)以以下方式打开NIO服务器套接字通道。

ServerSocketChannel.open().also {
    it.bind(InetSocketAddress(0))
}

但上述代码给我一个错误,提示需要API级别24。理论上,我可以使用ServerSocket然后结束,但我的客户端代码在使用SocketChannelByteBuffer等,混合使用这两者感觉设计不良!

还让我困惑的另一件事是,我可以在最低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.

huangapple
  • 本文由 发表于 2020年10月17日 18:57:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64401671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定