Examples of getting started with Java AIO

Source: Internet
Author: User

Java7 AIO Starter instance, the first is the server-side implementation:

Service-side code

Simpleserver:

Java code
  1. Public class Simpleserver {
  2. Public simpleserver (int port) throws IOException {
  3. final Asynchronousserversocketchannel listener = Asynchronousserversocketchannel.open (). Bind (New  Inetsocketaddress (port));
  4. Listener.accept (null, new Completionhandler<asynchronoussocketchannel, void> () {
  5. public void completed (Asynchronoussocketchannel ch, void att) {
  6. //Accept Next Connection
  7. Listener.accept (null, this );
  8. //Handle Current connection
  9. Handle (CH);
  10. }
  11. public void failed (Throwable exc, void att) {
  12. }
  13. });
  14. }
  15. public void handle (Asynchronoussocketchannel ch) {
  16. Bytebuffer Bytebuffer = bytebuffer.allocate (32);
  17. try {
  18. Ch.read (Bytebuffer). get ();
  19. } catch (Interruptedexception e) {
  20. //TODO auto-generated catch block
  21. E.printstacktrace ();
  22. } catch (Executionexception e) {
  23. //TODO auto-generated catch block
  24. E.printstacktrace ();
  25. }
  26. Bytebuffer.flip ();
  27. System.out.println (Bytebuffer.get ());
  28. //Do something
  29. }
  30. }
followed by the client implementation: client code

SimpleClient:

Java code
  1. Public class SimpleClient {
  2. private Asynchronoussocketchannel client;
  3. Public simpleclient (String host, int port) throws IOException, Interruptedexception, executionexception {
  4. this.client = Asynchronoussocketchannel.open ();
  5. future<?> future = Client.connect (new Inetsocketaddress (host, port));
  6. Future.get ();
  7. }
  8. public void write (byte b) {
  9. Bytebuffer Bytebuffer = bytebuffer.allocate (32);
  10. Bytebuffer.put (b);
  11. Bytebuffer.flip ();
  12. Client.write (Bytebuffer);
  13. }
  14. }

Write a simple test case to run the server and the client, running TestServer () first, running TestClient ();

Test Cases

Aiotest

Java code
  1. Public class Aiotest {
  2. @Test
  3. public void TestServer () throws IOException, interruptedexception {
  4. Simpleserver Server = new Simpleserver (7788);
  5. Thread.Sleep (10000);
  6. }
  7. @Test
  8. public void TestClient () throws IOException, Interruptedexception, executionexception {
  9. SimpleClient client = new SimpleClient ("localhost", 7788);
  10. Client.write ((byte) 11);
  11. }
  12. }

Because it is asynchronous, there is no synchronization blocking when running the server, where I add a thread sleep (), if not, the program will run out of garbage.

http://tigerlchen.iteye.com/blog/1747221

Java AIO Getting Started instance (GO)

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.