I have an Android app that has a Socket open. I would like to write to this socket without any possibility of blocking the thread on the write for a noticeable amount of time. If any IO error occurs, I would like the write to just silently fail. Is there an easy way to do this?
Yes, NIO provides a SocketChannel
class (call the getChannel
method on your Socket
), which allows you to call configureBlocking
to use non-blocking mode. You should then do all your I/O through the channel, and not through the Socket
object.
You might want to consider looking at NIO:
http://developer.android.com/reference/java/nio/channels/package-summary.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.