Java basic knowledge of network programming ——-AIO-based asynchronous socket communication

Source: Internet
Author: User

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,

    1. Call the open static method to create the Asynchronousserversocketchannel
    2. Call Asynchronousserversocketchannel's Bind method to listen for the specified IP and port
    3. 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

Related Article

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.