网络编程复习(四):AIO
发布日期:2021-11-13 10:21:49
浏览次数:8
分类:技术文章
本文共 4091 字,大约阅读时间需要 13 分钟。
AIO理解起来有点绕,本人也懒得用,这都是小demo,了解下概念和流程就可以了,实际应用是不会这么用的,因为多次读写都可能会导致粘包,不完全度等等一系列的问题。
Client:
package 网络编程_AIO;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.AsynchronousSocketChannel;public class Client implements Runnable { private AsynchronousSocketChannel channel = null; public Client() throws Exception { channel = AsynchronousSocketChannel.open(); } public void connect() { channel.connect(new InetSocketAddress("127.0.0.1", 8888)); } public void wirte(String request) { try { channel.write(ByteBuffer.wrap(request.getBytes())).get(); read(); } catch (Exception e) { e.printStackTrace(); } } private void read() { ByteBuffer readBuffer = ByteBuffer.allocate(1024); try { channel.read(readBuffer).get(); readBuffer.flip(); byte[] bys = new byte[readBuffer.remaining()]; readBuffer.get(bys); System.out.println(new String(bys, "utf-8").trim()); } catch (Exception e) { e.printStackTrace(); } } @Override public void run() { while (true) { } } public static void main(String[] args) throws Exception { Client c1 = new Client(); Client c2 = new Client(); Client c3 = new Client(); c1.connect(); c2.connect(); c3.connect(); new Thread(c1,"c1").start(); new Thread(c2,"c2").start(); new Thread(c3,"c3").start(); Thread.sleep(1000); c1.wirte("c1 say hello"); c2.wirte("c2 say fuck"); c3.wirte("c3 say good afternoon"); }}server:
package 网络编程_AIO;import java.net.InetSocketAddress;import java.nio.channels.AsynchronousChannelGroup;import java.nio.channels.AsynchronousServerSocketChannel;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Server { private ExecutorService executorService; private AsynchronousChannelGroup group; public AsynchronousServerSocketChannel socketChannel; public Server(int port){ try { executorService = Executors.newCachedThreadPool(); group = AsynchronousChannelGroup.withCachedThreadPool(executorService, 1); socketChannel = AsynchronousServerSocketChannel.open(group); socketChannel.bind(new InetSocketAddress(port)); socketChannel.accept(this,new ServerCompeletionHandler()); Thread.sleep(Integer.MAX_VALUE); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unused") public static void main(String[] args) { Server server = new Server(8888); }}ServerCompletionHandler:
package 网络编程_AIO;import java.nio.ByteBuffer;import java.nio.channels.AsynchronousSocketChannel;import java.nio.channels.CompletionHandler;public class ServerCompeletionHandler implements CompletionHandler输出结果:{ @Override public void completed(AsynchronousSocketChannel result, Server attachment) { attachment.socketChannel.accept(attachment,this); read(result); } private void read(AsynchronousSocketChannel result) { ByteBuffer buffer = ByteBuffer.allocate(1024); result.read(buffer,buffer,new CompletionHandler () { @Override public void completed(Integer resultSize, ByteBuffer attachment) { attachment.flip(); System.out.println("Server:接受的到的客户端长度为"+resultSize); String resultData = new String(attachment.array()).trim(); System.out.println("Server:接收到的客户端数据为:"+resultData); String response = new String("服务端响应,接收到客户端数据为:"+resultData); write(result,response); } @Override public void failed(Throwable exc, ByteBuffer attachment) { exc.printStackTrace(); } }); } protected void write(AsynchronousSocketChannel result, String response) { try { ByteBuffer wirteBuffer = ByteBuffer.allocate(1024); wirteBuffer.put(response.getBytes()); wirteBuffer.flip(); result.write(wirteBuffer).get(); } catch (Exception e) { e.printStackTrace(); } } @Override public void failed(Throwable exc, Server attachment) { exc.printStackTrace(); }}
client:
服务端响应,接收到客户端数据为:c1 say hello服务端响应,接收到客户端数据为:c2 say fuck服务端响应,接收到客户端数据为:c3 say good afternoonserver:
Server:接受的到的客户端长度为12Server:接收到的客户端数据为:c1 say helloServer:接受的到的客户端长度为11Server:接收到的客户端数据为:c2 say fuckServer:接受的到的客户端长度为21Server:接收到的客户端数据为:c3 say good afternoonAIO是JDK1.7之后提供的异步非阻塞网络通信,个人觉得还是麻烦,不好用,以后会提供更好的方式,框架Netty.
转载地址:https://blog.csdn.net/Lee_Ho_/article/details/78123595 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月16日 17时53分27秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
可能是全网最全的SpringBoot启动流程源码分析(基于 2.1.5 版本)
2019-04-27
BAT华为等一线大厂工程师都在用的优秀 IDEA 插件
2019-04-27
屏幕录制和编辑神器ScreenFlow轻松上手
2019-04-27
防止Java序列化/反射破坏单例模式的解决方案
2019-04-27
面试/工作必备的vim基础及快捷键操作
2019-04-27
@SpringBootApplication注解到底做了什么,你真的了解吗?
2019-04-27
Java生态中性能最强数据库连接池HikariCP
2019-04-27
Restful Web Service设计规范
2019-04-27
设计模式实战-状态模式(State Pattern)
2019-04-27
同步更新 github上fork别人的项目的最新提交
2019-04-27
一文讲明Dubbo Remoting模块
2019-04-27
OAuth 2.0实战(一)-通俗光速入门
2019-04-27
OAuth 2.0实战(二)-为什么要先获取授权码code?
2019-04-27
授权服务是如何颁发授权码和访问令牌的?
2019-04-27
OAuth2.0实战(三)-使用JWT
2019-04-27
你确定懂OAuth 2.0的三方软件和受保护资源服务?
2019-04-27