简体   繁体   中英

Non-blocking write to a socket in Android

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM