lacker
lacker

Reputation: 5550

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?

Upvotes: 1

Views: 3548

Answers (2)

paulsm4
paulsm4

Reputation: 121649

You might want to consider looking at NIO:

http://developer.android.com/reference/java/nio/channels/package-summary.html

Upvotes: 2

C. K. Young
C. K. Young

Reputation: 223023

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.

Upvotes: 3

Related Questions