Netty学习——实战篇1 BIO、NIO入门demo 备注

 1 BIO 实战代码 

@Slf4j
public class BIOServer {public static void main(String[] args) throws IOException {//1 创建线程池ExecutorService threadPool = Executors.newCachedThreadPool();//2 创建ServerSocketServerSocket serverSocket = new ServerSocket(8000);log.info("服务器已启动成功");//3 监听,等待客户端连接while(true){log.info("线程id:{}",Thread.currentThread().getId());log.info("等待连接...");//4 客户端连接后,创建一个线程,与客户端通信final Socket socket = serverSocket.accept();threadPool.execute(new Runnable() {@Overridepublic void run() {handler(socket);}});}}//编写一个handler方法,与客户端通信public static  void handler(Socket socket){try {log.info("线程id:{}",Thread.currentThread().getId());//创建一个byte[]数组byte[] bytes = new byte[1023];//通过socket获取输入流InputStream inputStream = socket.getInputStream();//循环读取客户端发送的消息while(true){int read = inputStream.read(bytes);if(read != -1){System.out.println(new String(bytes,0,read));}else{break;}}}catch (Exception e){e.printStackTrace();}finally {try {log.info("关闭与客户端的连接");socket.close();} catch (IOException e) {throw new RuntimeException(e);}}}
}

        运行结果

8621025cda1d47839eed300130b406cd.png

2 NIO实战

2.1 Buffer实战

@Slf4j
public class BasicBuffer {public static void main(String[] args) {//1 创建一个BufferIntBuffer buffer = IntBuffer.allocate(10);//2 往Buffer存储数据for (int i = 0; i < buffer.capacity(); i++) {buffer.put(i * 2);}//3 Buffer读写切换buffer.flip();//4 Buffer 读数据while(buffer.hasRemaining()){log.info("{}",buffer.get());}}
}

 

2.2 FileChannel实战

        使用NIO的FileChannel和ByteBuffer,把一段文字写入到指定的文件中。

//把文字写入到指定文件中
public class FileChannelWrite{public static void main(String[] args) throws IOException {//1 指定文字String str = "hello,孔乙己";//2 创建一个输出流FileOutputStream fileOutputStream = new FileOutputStream("D:\\develop\\java-base\\test\\test01.txt");//3 通过输出流创建FileChanneljava.nio.channels.FileChannel fileChannel = fileOutputStream.getChannel();//4 创建一个缓冲区ByteBuffer buffer = ByteBuffer.allocate(1024);//5 把文字放入缓冲区buffer中buffer.put(str.getBytes());//6 对buffer进行翻转buffer.flip();//7 把buffer的数据写入到FileChannelfileChannel.write(buffer);//8 关闭流fileOutputStream.close();}
}

 

        使用NIO的FileChannel和Bytebuffer,读取指定文件中的内容,并打印到控制台

@Slf4j
public class FileChannelRead {public static void main(String[] args) throws IOException {//1 创建输入流File file = new File("D:\\develop\\java-base\\test\\test01.txt");FileInputStream fileInputStream = new FileInputStream(file);//2 通过输入流创建FileChannelFileChannel fileChannel = fileInputStream.getChannel();//3 创建缓冲区ByteBuffer buffer = ByteBuffer.allocate((int) file.length());//4 把通道的数据读取到缓冲区fileChannel.read(buffer);//5 把bytebuffer 转成 stringlog.info("文件的内容是: {}",new String(buffer.array()));//6 关闭输入流fileInputStream.close();}
}

 

        使用一个Buffer,FileChannel和read/write方法,完成文件的拷贝 .

public class FileChannelCopy {public static void main(String[] args) throws IOException {FileInputStream fileInputStream = new FileInputStream("D:\\develop\\java-base\\test\\source.txt");FileChannel inputChannel = fileInputStream.getChannel();FileOutputStream fileOutputStream = new FileOutputStream("D:\\develop\\java-base\\test\\target.txt");FileChannel outChannel = fileOutputStream.getChannel();ByteBuffer buffer = ByteBuffer.allocate(1024);while (true){buffer.clear();int read = inputChannel.read(buffer);if(read == -1){break;}buffer.flip();outChannel.write(buffer);}fileInputStream.close();fileOutputStream.close();}
}

        使用transferFrom方法拷贝文件 

public class FileChannelCopy01 {public static void main(String[] args) throws IOException {FileInputStream fileInputStream = new FileInputStream("D:\\develop\\java-base\\test\\source.png");FileOutputStream fileOutputStream = new FileOutputStream("D:\\develop\\java-base\\test\\dest.png");FileChannel inChannel = fileInputStream.getChannel();FileChannel outChannel = fileOutputStream.getChannel();outChannel.transferFrom(inChannel,0,inChannel.size());fileInputStream.close();fileOutputStream.close();}
}

2.3 MappedByteBufer

         MappedByteBufer 可以让文件直接在内存(堆外内存)修改,操作系统不需要再拷贝一次

//原来的内容是:hello,孔乙己
//修改后的内容是:HelLo,孔乙己
public class MappedByteBuffer {public static void main(String[] args) throws IOException {RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\develop\\java-base\\test\\test01.txt", "rw");FileChannel channel = randomAccessFile.getChannel();java.nio.MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);map.put(0,(byte) 'H');map.put(3,(byte) 'L');randomAccessFile.close();}
}

2.4 ServerSocketChannel和SocketChannel

@Slf4j
public class ServerSocketChannelAndSocketChannel {public static void main(String[] args) throws IOException {//1 使用ServerSocketChannel和SocketChannel 网络ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();InetSocketAddress address = new InetSocketAddress(8000);//2 绑定端口到socket,并启动serverSocketChannel.socket().bind(address);//3 创建 ByteBuffer数组ByteBuffer[] buffers = new ByteBuffer[2];buffers[0] = ByteBuffer.allocate(5);buffers[1] = ByteBuffer.allocate(3);//4 等待客户端连接SocketChannel socketChannel = serverSocketChannel.accept();int messageLength = 8;//假定从客户端接收到8个字节//5 循环读取while(true){int byteRead = 0;while(byteRead < messageLength){long read = socketChannel.read(buffers);byteRead += read;log.info("byteRead = ,{}",byteRead);//使用流打印,查看当前的这个buffer的position和limitArrays.asList(buffers).stream().map(buffer -> "position = " +buffer.position() +",limit = " + buffer.limit()).forEach(System.out::println);}// 6 将所有的buffer进行翻转Arrays.asList(buffers).forEach(buffer -> buffer.flip());// 将数据读取到客户端long byteWrite = 0;while(byteWrite < messageLength){long l = socketChannel.write(buffers);byteWrite += l;}//将所有的buffer进行翻转Arrays.asList(buffers).forEach(buffer ->{buffer.clear();});log.info("byteRead = ,{},byteWrite = {},messageLength = {}",byteRead,byteWrite,messageLength);}}
}

2.5 使用NIO开发服务端和客户端

        NIOServer.java 

 

@Slf4j
public class NIOServer {public static void main(String[] args) throws Exception{//1 创建ServerSocketChannelServerSocketChannel serverSocketChannel = ServerSocketChannel.open();//2 创建Selector对象Selector selector = Selector.open();//3 绑定端口 8000,在服务器监听serverSocketChannel.socket().bind(new InetSocketAddress(8000));//4 ServerSocketChannel设置为非阻塞serverSocketChannel.configureBlocking(false);//5 把ServerSocketChannel的OP_ACCEPT注册到selectorserverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);log.info("ServerSocketChannel已注册到Selector上");// 6 循环,等待客户端连接while(true){//7 等待一秒,如果没有事件发生,就返回if(selector.select(1000) == 0){log.info("等待1秒,没有客户端连接");continue;}//8 如果有事件发生,就获取selectedKyes,返回关注事件的集合Set<SelectionKey> selectededKeys = selector.selectedKeys();Iterator<SelectionKey> keyIterator = selectededKeys.iterator();//9 遍历selectedKeys集合,获取事件类型while(keyIterator.hasNext()){SelectionKey key = keyIterator.next();if(key.isAcceptable()){log.info("客户端连接成功");//10_1 如果是OP_ACCEPT事件,证明有新的客户端连接,为该客户端创建一个SocketChannelSocketChannel socketChannel = serverSocketChannel.accept();socketChannel.configureBlocking(false);//10_1_1 将SocketChannel注册到selector上,事件类型是OP_READ,并绑定一个缓冲区socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(1024));}if(key.isReadable()){//10_2  如果是OP_READ事件,通过selectedKey 反向获取 SocketChannelSocketChannel channel =(SocketChannel) key.channel();//10_2_1  获取该channel绑定的buffer,并读取ByteBuffer buffer = (ByteBuffer)key.attachment();channel.read(buffer);log.info("从客户端读取的数据是: {}",new String(buffer.array()));}//11 移除selectedKey,防止重复操作keyIterator.remove();}}}
}

        NIOClient.java

 

@Slf4j
public class NIOClient {public static void main(String[] args) throws Exception {//1 创建SocketChannelSocketChannel socketChannel = SocketChannel.open();//2 设置非阻塞模式socketChannel.configureBlocking(false);//3  提供服务器的ip和端口InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8000);//4 连接服务器if(!socketChannel.connect(address)){// 4_1 连接失败while(!socketChannel.finishConnect()){log.info("连接需要时间,此时客户端不会阻塞,可以做其他工作。。。");}}//4_2 连接成功,发送数据String str = "hello ,孔乙己";ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());socketChannel.write(buffer);System.in.read();}
}

        运行结果:

cac6746784044121871985398c1944f7.png

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/611378.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【springCloud】版本学习

Spring Cloud介绍 官网地址&#xff1a;https://spring.io/projects/spring-cloud Spring Cloud 是一个基于 Spring Boot 的微服务架构解决方案&#xff0c;它提供了一系列工具和模式来帮助开发者构建分布式系统。Spring Cloud 的组件和模式包括配置管理、服务发现、断路器、…

瀑布流布局

瀑布流布局&#xff1a;瀑布流&#xff0c;又称瀑布流式布局&#xff0c;是比较流行的一种页面布局&#xff0c;视觉表现为参差不齐的多栏布局。 问题概述: 一次性生成&#xff0c;不需要再次增加&#xff0c;排序顺序由上倒下&#xff0c;由左到右 解决方案&#xff1a; //…

谷歌AI新玩意:一场名为Gemini Code Assist的编程辅助革命

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

Springboot+vue的粮仓管理系统的设计与实现(有报告)。Javaee项目,springboot vue前后端分离项目。

演示视频&#xff1a; Springbootvue的粮仓管理系统的设计与实现&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot vue前后端分离项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&am…

【Qt编译】ARM环境 Qt5.14.2-QtWebEngine库编译 (完整版)

ARM 编译Qt5.14.2源码 1.下载源码 下载Qt5.14.2源代码&#xff08;可根据自己的需求下载不同版本&#xff09; 下载网站&#xff1a;https://download.qt.io/new_archive/qt/5.14/5.14.2/single/ 2.相关依赖(如果需要的话) 先参考官方文档的需求进行安装&#xff1a; 官方…

React-样式使用

​&#x1f308;个人主页&#xff1a;前端青山 &#x1f525;系列专栏&#xff1a;React篇 &#x1f516;人终将被年少不可得之物困其一生 依旧青山,本期给大家带来React篇专栏内容:React-样式使用 目录 1、行内样式 2、使用className属性 3、css module模块化 4、styled-c…

力扣HOT100 - 48. 旋转图像

解题思路&#xff1a; 要求原地旋转 可以先上下翻转&#xff0c;再沿主对角线反转&#xff08;左上到右下的对角线&#xff09; class Solution {public void rotate(int[][] matrix) {int n matrix.length;// 上下翻转for (int i 0; i < n / 2; i) {for (int j 0; j &…

C语言—每日选择题—Day69

第一题 1、以下程序的输出结果是&#xff08; &#xff09; int main() {char arr[2][4];strcpy (arr[0],"you");strcpy (arr[1],"me");arr[0][3]&;printf("%s \n",arr);return 0; } A: you&me B: you C: me D: err 答案及解析 A 这里重…

python 如何获得重定向输入

通过内置的fileinput模块即可实现&#xff0c;创建文件filein.py&#xff1a; #!/usr/bin/env python import fileinput f_input fileinput.input() for line in f_input:print(line, end)增加可执行权限&#xff1a; chmod ax filein.py 使用&#xff1a; $ ls | ./filein.py…

3-1 AUTOSAR RTE概述

返回总目录->返回总目录<- 一、概念 1.1 虚拟总线VFB 若从整车级别去看待整车上所有的功能模块,即软件组件的架构,它们之间的通信形式主要涉及以下两种: 在单个ECU内部的通信(Intra-ECU Communication);在多个ECU之间的通信(Inter-ECU Communication)。 …

【Leetcode每日一题】 动态规划 - 下降路径最小和(难度⭐⭐)(55)

1. 题目解析 题目链接&#xff1a;931. 下降路径最小和 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了. 2.算法原理 对于这类路径类问题&#xff0c;通常我们首先需要分析状态表示以及状态转移的过程。特别地&#xff0c;本题涉及…

[大模型]Qwen1.5-7B-Chat-GPTQ-Int4 部署环境

Qwen1.5-7B-Chat-GPTQ-Int4 部署环境 说明 Qwen1.5-72b 版本有BF16、INT8、INT4三个版本&#xff0c;三个版本性能接近。由于BF16版本需要144GB的显存&#xff0c;让普通用户忘却止步&#xff0c;而INT4版本只需要48GB即可推理&#xff0c;给普通用户本地化部署创造了机会。&…