Java AIO 入门实例

原文出处:http://tigerlchen.iteye.com/blog/1747221

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

服务端代码

SimpleServer:

Java代码 复制代码 收藏代码
  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. // 接受下一个连接
  7. listener.accept(null, this);
  8. // 处理当前连接
  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. }
public class SimpleServer {

    public SimpleServer(int port) throws IOException {
        final AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port));

        listener.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
            public void completed(AsynchronousSocketChannel ch, Void att) {
                // 接受下一个连接
                listener.accept(null, this);

                // 处理当前连接
                handle(ch);
            }

            public void failed(Throwable exc, Void att) {

            }
        });

    }

    public void handle(AsynchronousSocketChannel ch) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(32);
        try {
            ch.read(byteBuffer).get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byteBuffer.flip();
        System.out.println(byteBuffer.get());
        // Do something
    }
    
}

跟着是客户端实现:
客户端代码

SimpleClient:

Java代码 复制代码 收藏代码
  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. }
public class SimpleClient {
    
    private AsynchronousSocketChannel client;
    
    public SimpleClient(String host, int port) throws IOException, InterruptedException, ExecutionException {
        this.client = AsynchronousSocketChannel.open();
        Future<?> future = client.connect(new InetSocketAddress(host, port));
        future.get();
    }
    
    public void write(byte b) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(32);
        byteBuffer.put(b);
        byteBuffer.flip();
        client.write(byteBuffer);
    }

}

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

测试用例

AIOTest

Java代码 复制代码 收藏代码
  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. }
public class AIOTest {
    
    @Test
    public void testServer() throws IOException, InterruptedException {
        SimpleServer server = new SimpleServer(7788);
        
        Thread.sleep(10000);
    }
    
    @Test
    public void testClient() throws IOException, InterruptedException, ExecutionException {
        SimpleClient client = new SimpleClient("localhost", 7788);
        client.write((byte) 11);
    }

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值