Java Aio Socket异步通信例子

以下是关于Aio Socket 通信的一个例子,通过回调的方式发送和处理消息。其中要注意的是在缓冲区(ByteBuffer)中put数据以后要先进行flip(参见Nio部分解释)操作再发送数据,否则数据将会传输失败。以下是例子,直接上代码。

服务端实现:

public class AioServer {
    private AsynchronousServerSocketChannel server;
    private int port;
    private ByteBuffer send = ByteBuffer.allocate(2048);
    private ByteBuffer recive = ByteBuffer.allocate(2048);

    public AioServer(int port) throws IOException {
        server = AsynchronousServerSocketChannel.open();
        this.port = port;
    }

    public void startup() throws Exception {
        System.out.println("========服务已启动=========");
        AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(4));
        server.bind(new InetSocketAddress(port));
        // 监听客户端连接
        server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {

            @Override
            public void completed(AsynchronousSocketChannel result, Void attachment) {

                try {
                    recive.clear();
                    Integer n = result.read(recive).get();
                    System.out.println("size:"+n+">已捕捉到连接-消息:"+new String(recive.array()));
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }; //接收客户端数据

                send.clear();
                send.put("处理成功".getBytes());
                send.flip();
                result.write(send); //反馈结果
                server.accept(null, this);  //这里是递归使服务器保持监听状态

            }


            @Override
            public void failed(Throwable exc, Void attachment) {

            }

        });

        group.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        try {
            new AioServer(8000).startup();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

客户端实现:

public class AioClient {

    private int port;
    private ByteBuffer send = ByteBuffer.allocate(2048);
    private ByteBuffer recive = ByteBuffer.allocate(2048);
    private boolean isDone = false;

    public AioClient(int port) {
        this.port = port;
    }

    public void send(String message) throws IOException {
        System.out.println("正在发送...");
        AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
        client.connect(new InetSocketAddress(port), null, new CompletionHandler<Void, Void>() {

            @Override
            public void completed(Void result, Void attachment) {
                send.clear();
                send.put(message.getBytes());
                send.flip();
                recive.clear();
                // 写
                client.write(send, null, new CompletionHandler<Integer, Void>() {
                    @Override
                    public void completed(Integer result, Void attachment) {

                        // 读取结果
                        try {
                            Integer integer = client.read(recive).get();
                            System.out.println("size:"+integer+"> 收到结果:"+new String(recive.array()));
                            isDone = true;
                        } catch (InterruptedException | ExecutionException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void failed(Throwable exc, Void attachment) {

                    }
                });

            }

            @Override
            public void failed(Throwable exc, Void attachment) {

            }
        });

        while (isDone == false) {
            try {
                Thread.sleep(1);// 这里起到wait的作用,不做点事情此处会被优化为wait 会一直挂起
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            new AioClient(8000).send("客户端的数据包");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值