开发者

Java Non Blocking Client

I´ve a question concerning non blocking Sockets: I understand how to register for example, two socketchannels for write/read events.

But how does such an event look like? If I want to write some dat开发者_如何学编程a on SocketChannel1 (for example when I press a button) to a server how can I do this?

All examples I´ve found only deal with the registration of the sockets, like this: http://rox-xmlrpc.sourceforge.net/niotut/#About%20the%20author

Greetings,

Flo


I would look at the examples which come with the JDK under the sample directory.

If you use non blocking IO, you should wait until after you have a write op from the socket to perform the write. While you are waiting, you can buffer the data. However, this rarely needed as this is only required when the write buffer of the socket is full (which shouldn't happen very often) and if this is the case for a long period fo time you may deside you have a slow consumer and close the connection instead.

Personally, I wouldn't suggest you use non-blocking NIO directly unless you have a very good understanding of what is going on. Instead I suggest you use a library like Netty which will handle all the edge cases for you. Or you could use blocking NIO which is much simpler (and can be faster for a small number of connections)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜