Java nio tcp Programming

Source: Internet
Author: User

Original Source: http://navigating.blogbus.com/logs/18024423.html

Before Java, Java Network Programming only had blocking methods. After Java and later, Java provided non-blocking network programming APIs. from the perspective of Java development, due to the rapid development of Java and the improvement of JVM performance, more and more applications are involved in server application development, and more network applications require high performance, this is the main reason for non-blocking network programming in Java.
For me, most of the previous server applications were mainly built on the application server. Therefore, communication is implemented and managed by the application server. This time, due to communication and protocol, we must implement a Java server program capable of processing a large number of concurrent clients with high performance parallel processing. Therefore, it is inevitable to select a non-blocking processing method. First, let's look at the blocking processing method:
In the blocked network programming method, for each individual network connection, a thread must be bound to the network connection to process the network byte stream. The following is a piece of code:
Public static void main (string [] ARGs ){
Try {
Serversocket SSC = new serversocket (23456 );
While (true ){
System. out. println ("Enter Accept :");
Socket s = ssc. accept ();
Try {
(New Thread (new Worker (s). start ();
} Catch (Exception e ){
// TODO
E. printStackTrace ();
}
}
} Catch (ioexception e ){
E. printstacktrace ();
}

}

Public static class worker implements runnable {
Private socket S;

Private Boolean running = true ;;

Public worker (socket s ){
This. S = s;
}

Public void run (){
Try {
InputStream is = s. getInputStream ();
OutputStream OS = s. getOutputStream ();
While (running ){
Byte [] B = this. readByLength (is, 1024 );
This. process (B );
}
} Catch (Throwable t ){
// TODO
T. printStackTrace ();
}
}

Private byte [] readByLength (InputStream is, int contLen) throws IOException {
Byte [] B = new byte [contLen];
Int off = 0;
Int length = 0;
While (length = is. read (B, off, contLen-off)> = 0 ){
Off = + length;
If (off> = contLen ){
Break;
}
}
Return B;
}

Private void process (byte [] B ){

}
}
In this code, we can see two blocking methods: The ServerSocket accept () method; and the InputStream read () method. Therefore, we need to process two types of threads separately. In addition, the thread lifecycle bound to each blocking method is consistent with that of the network connection. Based on the above reasons, NIO came into being. On the one hand, it establishes a thread correspondence for each network connection, and each thread has a large number of threads in the Idle State other than read/write, so we hope to reduce the number of threads, reducing the idle state and improving the running and execution efficiency of a single thread is actually making full use of the computing and running capabilities of the CPU (because if there are a large number of links, there are a large number of threads, and a large number of threads are blocked in the read () or write () method. At the same time, the CPU needs to schedule and switch back and forth frequently among these threads, it will inevitably bring about a lot of system calls and resource competition .); in addition, we hope to improve the performance of network I/O and hard disk I/O operations. There are three new features in NIO:
1. data Buffer processing (bytebuffer): Because the original data communication type between the operating system and the application is byte, it is also the basic unit of Io data operations. In NiO, every basic native type (except Boolean) has buffer implementation: charbuffer, intbuffer, doublebuffer, protocol buffer, longbuffer, floatbuffer, and bytebuffer, data Buffer enables continuous processing of data streams in Io operations. There are two types of bytebuffer: Direct bytebuffer and nondirect bytebuffer. bytebuffer is a common Java object and follows the rules of objects in the Java heap; direct bytebuffer is a native code. Its memory allocation is not in the java stack and is not affected by Java memory collection. Each direct bytebuffer is a continuous memory space allocated directly, it is also an important way for NiO to improve performance. Another important feature of data buffering is that one or more logical view buffers can be created based on one data buffer ). for example, using view buffer, you can replace a byte buffer with an int buffer, or convert a large buffer to many small buffers. The view buffer is called because the conversion is only logical and there is no physical buffer creation. This makes it easier for us to operate the buffer.
2. asynchronous channel: a channel is an object with a lot of local code closely integrated with the operating system. Channel is used to implement non-blocking operations for network programming. It also effectively integrates with bytebuffer and socket to fully utilize the features of non-blocking and bytebuffer. We will see the specific socketchannel usage later.
3. Conditional Selection: Most Operating Systems Support APIs with conditional Selection to prepare IO channels. This ensures that a thread can effectively manage multiple IO channels at the same time. In NIO, the Selector (maintains the registered Channel and the status of these channels) and SelectableChannel (the Channel that can be managed by the Selector) and SelectionKey (SelectionKey identifies the ing relationship between Selector and SelectableChannel. Once a Channel is registered in Selector, A SelectionKey object is returned. SelectionKey stores two types of States: which operations are registered for the corresponding Channel, and which operations for the corresponding Channel are ready to perform corresponding data operations) to achieve this function.

The NIO package mainly contains the following abstract data types:

  • Buffer: Linear table structure that contains data and is used for reading and writing. A special class is also provided for I/O operations on memory ing files.
  • Charset: It allows you to exclude Unicode strings from byte sequences and perform backward ing.
  • Channels: Socket, file, and pipe pipelines are all full-duplex channels.
  • Selector: Multiple asynchronous I/O operations are concentrated in one or more threads (can be considered as the object-oriented version of the select () function in UNIX ).
Technorati labels: Java, NiO, TCP

A typical NIO non-blocking programming model is as follows:
Private Selector selector = null;

Private static final int BUF_LENGTH = 1024;

Public void start () throws IOException {
If (selector! = Null ){
Selector = Selector. open ();
}
ServerSocketChannel ssc = ServerSocketChannel. open ();
ServerSocket serverSocket = ssc. socket ();
ServerSocket. bind (new InetSocketAddress (80 ));
Ssc. configureBlocking (false );
Ssc. register (selector, SelectionKey. OP_ACCEPT );

Try {
While (true ){
Int nKeys = UnblockServer. this. selector. select ();

If (nKeys> 0 ){
Iterator it = selector. selectedKeys (). iterator ();
While (it. hasNext ()){
SelectionKey key = (SelectionKey) it. next ();
If (key. isAcceptable ()){
ServerSocketChannel server = (ServerSocketChannel) key. channel ();
Socketchannel channel = server. Accept ();
If (Channel = NULL ){
Continue;
}
Channel. configureBlocking (false );
Channel. register (selector, SelectionKey. OP_READ );
}

If (key. isReadable ()){
ReadDataFromSocket (key );
}

It. remove ();
}
}
}
} Catch (IOException ioe ){
Ioe. printStackTrace ();
}
}

/**
* @ Param key
* @ Throws IOException
*/
Private void readDataFromSocket (SelectionKey key) throws IOException {
ByteBuffer buf = ByteBuffer. allocate (BUF_LENGTH );
SocketChannel SC = (SocketChannel) key. channel ();

Int readBytes = 0;
Int ret;

Try {
While (ret = SC. read (buf. buf ()> 0 ){
ReadBytes + = ret;
}
} Finally {
Buf. flip ();
}

// Process buffer
// Buf. clear ();
}

From this program, we can basically understand some features of NIO network programming. The method for creating a SocketServer has changed and the non-blocking mode needs to be specified, you need to create a Channel and register it to the Selector. Similarly, the process of establishing a network connection is the same, and then there is a conditional Selection (Readiness Selection ). in this way, each thread only needs to process a type of network selection. In code, we find that the processing method and blocking are completely different. We need to completely rethink how to compile the network communication module:
1. Timeout of persistent connections (timeout). Because the API does not directly support the Timeout parameter setting function, we need to implement this function by ourselves.
2. How to Use Selector because the processing capability of each Selector is limited, you need to consider how to use multiple Selector in the process of handling a large number of links and messages.
3. in the case of non-blocking, neither read nor write is blocked. Therefore, you need to consider how to completely read the specified message. How to ensure that the network environment is not good, write data into Io.
4. How to Apply bytebuffer, and creating a large number of bytebuffer itself is resource-consuming. How can we effectively use bytebuffer? At the same time, bytebuffer operations need to be carefully considered, because there are position (), mark (), limit (), capacity and other methods.
5. Since each thread processes network connections and faces a series of network connections, it is necessary to consider how to better use and schedule multithreading. In terms of message processing, you also need to ensure a certain order. For example, the login message arrives first, only after the login message is processed, it is possible to process other types of messages on the same link.
6. Memory leakage in network programming.
There are about two concurrent threads in the NIO access Processing Framework:
1. selector thread. Each selector occupies one thread independently. Because each selector has limited processing capability, multiple selector instances are required to work in parallel.
2. For each link in ready state, the thread needs to process the corresponding message. For this type of message, concurrent threads need to work together for processing. In this process, message integrity may be constantly required. In addition, messages on each link may have a time series. Therefore, the processing may also require the corresponding time series.
Currently, the open-source NiO frameworks of the Community are implemented by Mina, Grizzly, NiO framework, quickserver, and xsocket. Among them, Mina and grizzly are the most active, and the code quality is also high. They are also very different in their implementation methods. (Most open-source Java servers have already used NiO to rewrite the network. )
Whether it is our self-implemented NiO network programming framework or development based on open-source frameworks such as Mina and grizzly, understand the benefits of NiO and the many types of problems that NiO programming needs to solve. Adequate and effective unit testing is a good helper for us to write NiO code :)

Resource:
Http://www.cis.temple.edu /~ Ingargio/cis307/readings/unix4.html # states
Java NiO
Glassfish-open-source Java EE application server

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.