zoukankan      html  css  js  c++  java
  • Java AIO 入门实例(转)

    Java7 AIO入门实例,首先是服务端实现:

    服务端代码

    SimpleServer:

    Java代码  收藏代码
    1. public class SimpleServer {  
    2.   
    3.     public SimpleServer(int port) throws IOException {  
    4.         final AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port));  
    5.   
    6.         listener.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {  
    7.             public void completed(AsynchronousSocketChannel ch, Void att) {  
    8.                 // 接受下一个连接  
    9.                 listener.accept(null, this);  
    10.   
    11.                 // 处理当前连接  
    12.                 handle(ch);  
    13.             }  
    14.   
    15.             public void failed(Throwable exc, Void att) {  
    16.   
    17.             }  
    18.         });  
    19.   
    20.     }  
    21.   
    22.     public void handle(AsynchronousSocketChannel ch) {  
    23.         ByteBuffer byteBuffer = ByteBuffer.allocate(32);  
    24.         try {  
    25.             ch.read(byteBuffer).get();  
    26.         } catch (InterruptedException e) {  
    27.             // TODO Auto-generated catch block  
    28.             e.printStackTrace();  
    29.         } catch (ExecutionException e) {  
    30.             // TODO Auto-generated catch block  
    31.             e.printStackTrace();  
    32.         }  
    33.         byteBuffer.flip();  
    34.         System.out.println(byteBuffer.get());  
    35.         // Do something  
    36.     }  
    37.       
    38. }  
     
    跟着是客户端实现:
    客户端代码

    SimpleClient:

    Java代码  收藏代码
    1. public class SimpleClient {  
    2.       
    3.     private AsynchronousSocketChannel client;  
    4.       
    5.     public SimpleClient(String host, int port) throws IOException, InterruptedException, ExecutionException {  
    6.         this.client = AsynchronousSocketChannel.open();  
    7.         Future<?> future = client.connect(new InetSocketAddress(host, port));  
    8.         future.get();  
    9.     }  
    10.       
    11.     public void write(byte b) {  
    12.         ByteBuffer byteBuffer = ByteBuffer.allocate(32);  
    13.         byteBuffer.put(b);  
    14.         byteBuffer.flip();  
    15.         client.write(byteBuffer);  
    16.     }  
    17.   
    18. }  

    写一个简单的测试用例来跑服务端和客户端,先运行testServer(),在运行testClient();

    测试用例

    AIOTest

    Java代码  收藏代码
    1. public class AIOTest {  
    2.       
    3.     @Test  
    4.     public void testServer() throws IOException, InterruptedException {  
    5.         SimpleServer server = new SimpleServer(7788);  
    6.           
    7.         Thread.sleep(10000);  
    8.     }  
    9.       
    10.     @Test  
    11.     public void testClient() throws IOException, InterruptedException, ExecutionException {  
    12.         SimpleClient client = new SimpleClient("localhost", 7788);  
    13.         client.write((byte) 11);  
    14.     }  
    15.   
    16. }  

    因为是异步的,所以在运行server的时候没有发生同步阻塞,在这里我加了一个线程sleep(),如果没有的话,程序会直接跑完回收掉。

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

  • 相关阅读:
    27. 移除元素
    axios调用webapi报错
    MySql重装以后,修改数据库路径,打开以前的数据库报Table 'XX库.XX表' doesn't exist错误的解决办法
    SqlServer2012,设置指定数据库对指定用户开放权限
    win10无法访问服务器上的共享文件夹怎么设置,提示:你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
    Vs2017的git的坑
    jira6.3.6创建问题不自动发邮件通知的问题
    在windows下面配置redis集群遇到的一些坑
    SqlServer2008 无法修改表,超时时间已到 在操作完成之前超时解决方法
    小程序中也可以使用三元运算符且可嵌套使用
  • 原文地址:https://www.cnblogs.com/softidea/p/4575776.html
Copyright © 2011-2022 走看看