Java NIO on Android: Attempting to READ/WRITE while connection is still pending?

后端 未结 1 1133
不思量自难忘°
不思量自难忘° 2021-01-23 13:27

This question regards Java\'s NIO on Android (2.2, though I can build for higher APIs if necessary): After performing a SocketChannel connect() to a destination IP address, I re

相关标签:
1条回答
  • 2021-01-23 14:09

    You're doing it wrong.

    While the connection is pending, the channel must only be registered for OP_CONNECT.

    When OP_CONNECT fires, you must call finishConnect(), and then proceed as follows:

    1. If it returns true, you must then deregister OP_CONNECT, and you may then register OP_READ or OP_WRITE.

    2. If it returns false, do nothing: the connection is still pending.

    3. If it throws an exception, the connect has failed and you must close the channel.

    0 讨论(0)
提交回复
热议问题