Java Aio Socket异步通信例子
来源:互联网 发布:淘宝的穿衣助手在哪 编辑:程序博客网 时间:2024/06/07 11:07
以下是关于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(); } }}
阅读全文
0 0
- Java Aio Socket异步通信例子
- Java AIO-异步通信
- Java AIO-异步通信
- Java NIO(异步IO)Socket通信例子
- Aio--Java异步IO的 Socket Demo
- Aio--Java异步IO的 Socket Demo
- Aio--Java异步IO的 Socket Demo
- Aio--Java异步IO的 Socket Demo
- 一个异步Socket通信的例子(Windows)
- nio socket 异步通信 java
- java socket 简单通信例子
- Java Socket多线程通信例子
- JAVA AIO例子
- [VB.NET]询异步socket通信完善的例子.
- AIO实现非阻塞Socket通信
- Java AIO的Socket Demo
- 一个java Socket通信的例子
- Java Socket通信的简单例子
- 引用类型数组初始化
- [分表] -- mysql3重分表方式
- 企业发放的奖金来自利润提成。利润i低于或等于10万元的,可提成 10%为奖金···要求: (1) 用 if 语句编程序;(2) 用 switch 语句编程序。
- 《算法导论原书第二版(中文版带目录)》pdf
- IIS 如何发布网站
- Java Aio Socket异步通信例子
- Git 基本命令集合
- java判断一个序列是否可能是另外一个压栈序列的弹出序列
- vue2.0中goods选购栏滚动算法
- MySQL5.6之Index Condition Pushdown(ICP,索引条件下推)
- 大数据时代下的SQL Server第三方负载均衡方案----Moebius测试
- 求二叉树的高度
- 2017全球架构师峰会【 持续更新中~~】
- 2016 PJ 题解