Netty(一)之helloworld
HelloWorld客户端通向服务器端发送消息服务器端读取数据你好并且返回new Date()客户端读取数据pomdependency groupIdio.netty/groupId artifactIdnetty-all/artifactId version5.0.0.Alpha1/version /dependencyTimeServerHandler服务器端读取数据并且回应请求继承ChannelHandlerAdapter并且实现channelRead和exceptionCaught方法package myhelloworld; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import java.util.Date; /** * author CBeann * create 2019-08-27 18:31 */ public class TimeServerHandler extends ChannelHandlerAdapter { Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //服务器读客户端发送来的数据 ByteBuf buf (ByteBuf) msg; byte[] req new byte[buf.readableBytes()]; buf.readBytes(req); String body new String(req, UTF-8); System.out.println(The TimeServer receive : body); //服务器向客户端回应请求 ByteBuf response Unpooled.copiedBuffer(new Date().toString().getBytes()); ctx.writeAndFlush(response); } Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } // }TimeServerpackage myhelloworld; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; /** * author CBeann * create 2019-08-27 18:22 */ public class TimeServer { public static void main(String[] args) throws Exception { int port 8080; //配置服务器端的NIO线程组 EventLoopGroup bossGroup new NioEventLoopGroup(); EventLoopGroup workerGroup new NioEventLoopGroup(); try { ServerBootstrap b new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new ChannelInitializerSocketChannel() { Override protected void initChannel(SocketChannel socketChannel) throws Exception { //TimeClientHandler是自己定义的方法 socketChannel.pipeline().addLast(new TimeServerHandler()); } }); //绑定端口 ChannelFuture f b.bind(port).sync(); //等待服务端监听端口关闭 f.channel().closeFuture().sync(); } catch (Exception e) { } finally { //优雅关闭释放线程池资源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }启动NettyServer的模版代码private void bing(int port) { EventLoopGroup parentGroup new NioEventLoopGroup(); EventLoopGroup childGroup new NioEventLoopGroup(); try { ServerBootstrap b new ServerBootstrap(); b.group(parentGroup, childGroup) .channel(NioServerSocketChannel.class) //非阻塞模式 .option(ChannelOption.SO_BACKLOG, 128) .childHandler(new MyChannelInitializer()); ChannelFuture f b.bind(port).sync(); System.out.println(itstack-demo-netty server start done. {关注公众号bugstack虫洞栈获取源码}); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { childGroup.shutdownGracefully(); parentGroup.shutdownGracefully(); } }TimeClientHandler客户端读取服务器响应数据继承ChannelHandlerAdapter并且实现channelRead和exceptionCaught方法package myhelloworld; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.util.ReferenceCountUtil; /** * author CBeann * create 2019-08-27 18:47 */ public class TimeClientHandler extends ChannelHandlerAdapter { //客户端读取服务器发送的数据 Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try { ByteBuf buf (ByteBuf) msg; byte[] req new byte[buf.readableBytes()]; buf.readBytes(req); String body new String(req, UTF-8); System.out.println(Now is: body); } catch (Exception e) { } finally { //标配 ReferenceCountUtil.release(msg); } } Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } }TimeClientpackage myhelloworld; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; /** * author CBeann * create 2019-08-27 18:43 */ public class TimeClient { public static void main(String[] args) throws Exception { int port 8080; String host 127.0.0.1; //配置客户端NIO线程组 EventLoopGroup group new NioEventLoopGroup(); try { Bootstrap b new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializerSocketChannel() { Override protected void initChannel(SocketChannel socketChannel) throws Exception { //TimeClientHandler是自己定义的方法 socketChannel.pipeline().addLast(new TimeClientHandler()); } }); //发起异步连接操作 ChannelFuture f b.connect(host, port).sync(); //发送数据 f.channel().writeAndFlush(Unpooled.copiedBuffer(您好.getBytes())); //Thread.sleep(1000);//防止TCP粘包 //f.channel().writeAndFlush(Unpooled.copiedBuffer(您好.getBytes())); //Thread.sleep(1000); //f.channel().writeAndFlush(Unpooled.copiedBuffer(您好.getBytes())); //Thread.sleep(1000); //等待客户端链路关闭 f.channel().closeFuture().sync(); } catch (Exception e) { } finally { //优雅关闭 group.shutdownGracefully(); } } }学习总结1自己敲一遍因为不确定你查询到的博客是对还是错的2很多地方都是固定格式需要修改的地方就那么几个2.1option(ChannelOption.SO_BACKLOG, 1024) 修改一些参数2.2socketChannel.pipeline().addLast(new TimeServerHandler())添加一些自己定义或者系统的handler2.3自己定义的handler3请坚持

相关新闻

终极指南:如何免费解锁WeMod完整功能,获得专业版体验

终极指南:如何免费解锁WeMod完整功能,获得专业版体验

终极指南:如何免费解锁WeMod完整功能,获得专业版体验 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/GitHub_Trending/we/Wand-Enhancer 还在为WeMod专业版功能…

2026/7/28 18:21:10 阅读更多 →
Playwright自定义Fixtures:解决测试数据管理与环境隔离的工程实践

Playwright自定义Fixtures:解决测试数据管理与环境隔离的工程实践

1. 项目概述:为什么我们需要自定义 Fixtures?如果你在用 Playwright 做自动化测试,大概率已经熟悉了它的test函数和page对象。官方提供的test和page这些内置 Fixtures 确实方便,开箱即用。但当你开始构建一个稍具规模的测试套件时…

2026/7/28 18:21:10 阅读更多 →
Ryujinx模拟器终极指南:3步在PC上畅玩Switch游戏

Ryujinx模拟器终极指南:3步在PC上畅玩Switch游戏

Ryujinx模拟器终极指南:3步在PC上畅玩Switch游戏 【免费下载链接】Ryujinx 用 C# 编写的实验性 Nintendo Switch 模拟器 项目地址: https://gitcode.com/GitHub_Trending/ry/Ryujinx 想在电脑上体验Switch独占大作却不知从何入手?Ryujinx模拟器为…

2026/7/28 18:21:10 阅读更多 →

最新新闻

6岁患儿临床试验离世后:仇子龙Nature论文事件

6岁患儿临床试验离世后:仇子龙Nature论文事件

一则国际顶刊调查报道,揭开一场罕见病个体化基因编辑人体试验的悲剧;紧随其后,学术博主公开质疑相关《Nature》论文数据存在异常。上海交通大学医学院仇子龙团队 CHD3 碱基编辑研究事件,同时牵动人体临床试验伦理监管、学术论文数…

2026/7/28 18:32:13 阅读更多 →
for in

for in

var x var mycars new Array() mycars[0] “Saab” mycars[1] “Volvo” mycars[2] “BMW” for (x in mycars) { document.write(mycars[x] “”) }

2026/7/28 18:32:13 阅读更多 →
axure原型的使用

axure原型的使用

当我们开始学编程的时候,可以先了解一下axure,axure原型的使用可以让我们直观的了解我们的设计意图,接下来总结一下axure。 一、认识axure 1.元件(三大部分) 公共元件:box、h1-h3、image、label等 表单&…

2026/7/28 18:32:13 阅读更多 →
DamaiHelper:从手动抢票到智能掌控,你的演唱会门票管家

DamaiHelper:从手动抢票到智能掌控,你的演唱会门票管家

DamaiHelper:从手动抢票到智能掌控,你的演唱会门票管家 【免费下载链接】damaihelper 支持大麦网,淘票票、缤玩岛等多个平台,演唱会演出抢票脚本 项目地址: https://gitcode.com/gh_mirrors/dam/damaihelper 你是否曾经历过…

2026/7/28 18:32:13 阅读更多 →
聊一聊 .NET 的 AssemblyLoadContext 可插拔程序集

聊一聊 .NET 的 AssemblyLoadContext 可插拔程序集

聊一聊 .NET 的 AssemblyLoadContext 可插拔程序集 引言:程序集加载的痛点在传统的 .NET Framework 时代,应用程序域(AppDomain)提供了程序集隔离的能力,但它的使用较为复杂,且性能开销较大。到了 .NET Co…

2026/7/28 18:32:13 阅读更多 →
LangChain → AgentExecutor → LangGraph 的演进脉络

LangChain → AgentExecutor → LangGraph 的演进脉络

LangGraph 构建于 LangChain 之上,能极大简化智能体(Agent)与智能体运行时(Agent Runtime)的开发工作。 那么智能体和智能体运行时究竟指什么? 在 LangChain 中,我们对智能体的定义是&#xff1…

2026/7/28 18:31:13 阅读更多 →

日新闻

告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿!3步让你的暗影精灵笔记本重获新生 【免费下载链接】OmenSuperHub Control Omen laptop performance, fan speeds, and keyboard lighting, and unlock power limits. 项目地址: https://gitcode.com/gh_mirrors/om/OmenSuperHub 你是否也曾为官方Om…

2026/7/28 0:00:43 阅读更多 →
RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

做 RAG 的人应该都踩过这个致命的坑:把几百页的财报、法规、技术手册扔给向量库,问一个具体问题,搜出来的全是沾边但没用的内容 —— 关键信息要么被硬切块拆碎了,要么藏在几十条结果的最下面。语义相似≠真正相关,这个…

2026/7/28 0:00:43 阅读更多 →
抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

2026年做短视频运营,从抖音上扒文案早就不是偷偷抄笔记的事了。我刚开始做内容的时候,每天刷半小时抖音,手动把爆款视频的口播敲进备忘录,一条2分钟的视频得花十来分钟,碰到语速快的还要反复回听。后来试了一圈工具&am…

2026/7/28 0:00:43 阅读更多 →

周新闻

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 数据集6000张 完整源码已标注数据集训练好的模型环境配置教程程序运行说明文档,可以直接使用!系统支持图片、视频、摄像头等多种方式检测裂缝,功能强大实用。 1数据集6000张 8各类别

2026/7/28 12:04:22 阅读更多 →
深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

pubg数据集 精选原图1.42万数据 1.49万标签 无任何重复、算法增强或冗余图像! pubg绝地求生目标检测数据集 1分类:e_body,14905个标签,txt格式 共计14244张图,99%为640*640尺寸图像 适合yolo目标检测、AI训练关键词&am…

2026/7/28 8:29:16 阅读更多 →
Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex检测数据集数据集详情检测类别: allies enemy tag图片总量:7247张训练集:5139张验证集:1425张测试集:683张标注状态:全部已标注,即拿即用数据格式:支持YOLO格式及其他格式&#…

2026/7/28 5:03:42 阅读更多 →

月新闻