Detailed explanation of Java NiO server-side development

1、 Introduction to NiO class library

1. Buffer

Buffer is an object that contains some data to be written and read.

In NiO, all data is processed by buffer. When reading data, it is directly read from the channel to the buffer. When writing data, it is also written from the buffer to the channel.

The buffer is essentially an array, usually a byte array (ByteBuffer), or other types of arrays. In addition, the buffer also provides structured access to data and maintenance of read and write locations.

The inheritance relationship of buffer class is shown in the following figure:

2. Channel channel

Channel is a channel through which network data is read and written. The difference between channel and stream is that channel is bidirectional (channel can be used for reading and writing, and the latter can be carried out at the same time), and stream only moves in one direction.

Channels can be roughly divided into two categories: selectable channels for network reading and writing (serversocketchannel and socketchannel are their subclasses) and filechannels for file operations.

The following example shows how to write data to a file, read data from a file, and copy file data to another file through filechannel:

3. Multiplexer selector

The multiplexer provides the ability to select tasks that are ready. The selector will continuously poll the channels registered on it. If a channel sends read or write events, the channel will be in the ready state and will be polled by the selector. Then, through the selectionkey, you can obtain the set of ready channels for subsequent I / O operations.

A multiplexer selector can poll multiple channels at the same time. Because JDK uses epoll instead of the traditional select implementation, it has no limit on the maximum connection handle 1024 / 2048, which means that only one thread is responsible for the polling of the selector and can access tens of thousands of clients. The model is shown in the figure below:

Process a selector with a single thread. To use Selector, you have to register Channel with Selector, and then call its select () method. This method will block until an event is ready for a registered channel. Once this method returns, the thread can handle these events. Examples of events include new connection, data reception, etc.

Note:

1. What is the select model?

Select is an event triggering mechanism. It is triggered for processing when the waiting event occurs. It is mostly used for the processing of the server to the client implemented by Linux.

It can detect a group of IO devices that support non blocking at the same time and whether an event occurs (such as readable, writable, high priority error output, etc.) until a device triggers the event or exceeds the specified waiting time. That is, their responsibility is not to do IO, but to help the caller find the currently ready device.

2. What is the epoll model?

The design idea of epoll is to split a single select / poll operation into one epoll_ Create + multiple epolls_ CTRL + a wait. In addition, the kernel adds a file system "eventpollfs" for epoll operation. Each or more file descriptors to be monitored have an inode node of the corresponding eventpollfs file system, and the main information is saved in the eventpoll structure. The important information of the monitored file is saved in the epitem structure. So they are a one to many relationship.

2、 NiO server side development

Function Description: turn on the server and send a hello string to each accessed client.

Using NiO for server-side development mainly includes the following steps:

1. Create serversocketchannel and configure it as non blocking mode

2. Bind listening and configure TCP parameters, such as backlog size

3. Create an independent I / O thread for polling multiplexer selector

4. Create a selector, register the previously created serversocketchannel with the selector, and listen to the selectionkey ACCEPT

5. Start the I / O thread and execute the selector in the loop body The select () method polls the ready channels

6. When a ready channel is polled, it needs to be judged. If it is an op_ If the accept status indicates a new client access, call serversocketchannel The accept () method accepts the new client

7. Set the newly accessed client link socketchannel to non blocking mode and configure other TCP parameters

8. Register socketchannel with selector and listen to Op_ WRITE

9. If the polling channel is op_ Write indicates that to write data to the sockchannel, a ByteBuffer object is constructed to write data packets

The complete code is as follows:

We use telnet localhost 8080 to simulate multiple clients:

The running results of the program are as follows:

summary

The above is all about the detailed explanation of Java NiO server-side development in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>