Asynchronous IO
"Crazy Java Handout" from Li Gang below
According to the POSIX standard, the IO is divided into synchronous io and asynchronous IO. The IO operation is divided into two steps, 1) The program issues an IO request. 2) Complete the actual IO operation.
Both blocking IO and non-blocking IO are divided for the first step, and if an IO request is made to block the thread, it is blocking IO, otherwise it is non-blocking IO.
Synchronous IO and non-synchronous IO are for the second step, if the actual IO operation is done by the operating system and then returned to the program, it is asynchronous IO.
If the actual IO needs to be executed by the program itself, it will block the thread, which is synchronous IO.
JAVA7 's nio.2 provides an asynchronous channel, which makes it possible to communicate asynchronously to a network socket.
Using asynchronous IO Communication requires only three steps,
- Call the open static method to create the Asynchronousserversocketchannel
- Call Asynchronousserversocketchannel's Bind method to listen for the specified IP and port
- Calling the Accept method of Asynchronousserversocketchannel accepts a connection request
Here is a simple example,
Server-side
1 PackageAio;2 3 Importjava.io.IOException;4 Importjava.net.InetSocketAddress;5 ImportJava.nio.ByteBuffer;6 ImportJava.nio.channels.AsynchronousServerSocketChannel;7 ImportJava.nio.channels.AsynchronousSocketChannel;8 Importjava.util.concurrent.ExecutionException;9 Importjava.util.concurrent.Future;Ten One Public classServer { A Private Static Final intPORT = 3002; - Public Static voidMain (string[] args)throwsIOException, Interruptedexception, executionexception { - Try { theAsynchronousserversocketchannel Serverchannel =Asynchronousserversocketchannel.open (); -Serverchannel.bind (Newinetsocketaddress (PORT)); - while(true) { -future<asynchronoussocketchannel> future =serverchannel.accept (); + //get the Asynchronoussocketchannel after a successful connection -Asynchronoussocketchannel Socketchannel =future.get (); +Socketchannel.write (Bytebuffer.wrap ("Hello, this is the AIO world". GetBytes ("Utf-8")) . get (); A } at}Catch(IOException e) { - e.printstacktrace (); - } - } -}
Client
1 PackageAio;2 3 Importjava.io.IOException;4 Importjava.net.InetSocketAddress;5 ImportJava.nio.ByteBuffer;6 ImportJava.nio.channels.AsynchronousSocketChannel;7 ImportJava.nio.charset.Charset;8 Importjava.util.concurrent.ExecutionException;9 Ten Public classClient { One Private Static Final intPORT = 3002; A Public Static voidMain (string[] args)throwsIOException, Interruptedexception, executionexception { -Bytebuffer buff = bytebuffer.allocate (1024); -Charset UTF = Charset.forname ("Utf-8"); the Try { -Asynchronoussocketchannel Clientchannel =Asynchronoussocketchannel.open (); -Clientchannel.connect (NewInetsocketaddress ("127.0.0.1", PORT)). Get (); - buff.clear (); + clientchannel.read (Buff). get (); - Buff.flip (); +String content =utf.decode (Buff). toString (); ASYSTEM.OUT.PRINTLN ("Server info:" +content); at}Catch(IOException ex) { - ex.printstacktrace (); - } - } -}
Execution results, using a server-side and two-client tests,
Java basic knowledge of network programming ——-AIO-based asynchronous socket communication