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:
- Selectionkey.op_connect
- Selectionkey.op_accept
- Selectionkey.op_read
- 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