Java NiO for non-blocking socket communication

Source: Internet
Author: User

Bo Master Knowledge Level Limited, can only provide a narrow understanding of the individual, if there are newcomers read here, suggest to look at other tutorials or API, if you do not understand, look again, if there is Dalao read here, hope to point out the understanding of the problem ~ Thank you

Java provides sockets and ServerSocket packets for network communication, but the implementation is blocking, with only one connection at a time, which leads to a bad experience. Of course, we can also manage connections by constantly creating threads, but with more threads it can reduce efficiency. So Java introduced the non-blocking io--channel. And the channel provides the relevant channel for network communication.

The channel merges the traditional input and output streams, meaning that a channel can either input content or output content.

And, using the channel, the buffer provided by NIO should be used as an intermediary.

Channel Type:
    • FileChannel
    • Datagramchannel
    • Socketchannel
    • Serversocketchannel
Buffer type:
    • Bytebuffer
    • Charbuffer
    • DoubleBuffer
    • Floatbuffer
    • Intbuffer
    • Longbuffer
    • Shortbuffer

In order to use Socketchannel and Serversocketchannel, create a bytebuffer using Factory mode:

Bytebuffer Bytebuffer = bytebuffer.allocate (12);

To create the Channel method:

Serversocketchannel Serversocketchannel = Serversocketchannel.open ();

The following statement sets the specified channel to non-blocking:

Serversocketchannel.configureblocking (false);

Binding to a port:

Serversocketchannel.bind (new inetsocketaddress (PORT));

The corresponding if is Socketchannel, method is connect, here only take serversocketchannel example.

So far, we haven't seen how the channel implements non-blocking IO, so NIO provides a tool--selector.

Selector, in short, is a management tool for multiple channel, we can add multiple channel to a brain, if which channel is ready to read or write (or other operations) will notify us.

There is a interest concept in Selector, which means that when we register a channel with selector, we indicate what kind of behavior selector want to monitor the channel, after all, a channel can be read,write,accept, Connect

Selector instantiation:

Selector Selector = Selector.open ();

Channel Registration

Serversocketchannel.register (selector, selectionkey.op_accept);

The second argument is a interest type

Interest Type:
    1. Selectionkey.op_connect
    2. Selectionkey.op_accept
    3. Selectionkey.op_read
    4. Selectionkey.op_write

In general, we can register an accept for Serversocketchannel.

Then, when we call Selector.select (), selector waits for the channel that has been registered until a channel is ready. This process is blocking, of course, what is the relationship between blocking, in fact, IO or non-blocking.

We then refer to Selector.selectedkeys () to return a collection that contains a response channel, iteration set, and we get a channel.

Well, it's not about getting the channel, it's another thing--selectionkey.

Selectionkey contains a lot of information, you can get his selector,channel and registration information.

We can obtain information about our registration through selectionkey.isreadable,isacceptable,isconnectable,iswritable, and then deal with each type of response specifically.

Also, remember to remove the object after the iteration.

Java NiO for non-blocking socket communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.