网络编程复习(七):Netty解决拆包粘包问题--分隔符方式
发布日期:2021-11-13 10:21:51
浏览次数:5
分类:技术文章
本文共 4070 字,大约阅读时间需要 13 分钟。
在Netty中实现了2种拆包方式:分隔符方式,定长方式,这里介绍分隔符方式。实现起来很简单:
server:
package 网络编程_netty2;import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.string.StringDecoder;public class Server { private int port; public Server(int port){ this.port = port; } public void run()throws Exception{ EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup workers = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss, workers) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializerserverHandler:() { @Override public void initChannel(SocketChannel ch) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new ServerHandler()); } }); ChannelFuture f = bootstrap.bind(port).sync(); f.channel().closeFuture().sync(); workers.shutdownGracefully(); boss.shutdownGracefully(); } public static void main(String[] args) throws Exception { new Server(8888).run(); }}
package 网络编程_netty2;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;public class ServerHandler extends ChannelHandlerAdapter{ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String request = (String) msg; System.out.println(request); ctx.writeAndFlush(Unpooled.copiedBuffer("你好,服务端已经接收到你的请求$_".getBytes())); } }client
package 网络编程_netty2;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.string.StringDecoder;public class client { public static void main(String[] args) throws InterruptedException { EventLoopGroup worker = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(worker) .channel(NioSocketChannel.class) .handler(new ChannelInitializerclientHandler:() { @Override public void initChannel(SocketChannel ch) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new ClientHandler()); } }); ChannelFuture f = bootstrap.connect("127.0.0.1", 8888).sync(); f.channel().writeAndFlush(Unpooled.wrappedBuffer("aaa$_".getBytes())); f.channel().writeAndFlush(Unpooled.wrappedBuffer("bbbb$_".getBytes())); f.channel().writeAndFlush(Unpooled.wrappedBuffer("ccccccc$_".getBytes())); f.channel().closeFuture().sync(); worker.shutdownGracefully(); } }
package 网络编程_netty2;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;public class ClientHandler extends ChannelHandlerAdapter{ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String response = (String) msg; System.out.println(response); } }这里使用"$_"为分隔符。
转载地址:https://blog.csdn.net/Lee_Ho_/article/details/78135903 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月08日 18时37分35秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
在PyPI测试平台发布Python包
2019-04-26
中文代码示例之Electron桌面应用开发初体验
2019-04-26
中文代码示例之NW.js桌面应用开发初体验
2019-04-26
为《 两周自制脚本语言 》添加中文测试代码
2019-04-26
将《 两周自制脚本语言 》测试中使用的接口中文化
2019-04-26
5分钟入门LingaScript-尝鲜中文版TypeScript
2019-04-26
重拾《 两周自制脚本语言 》- 支持中文标识符
2019-04-26
Java实现文本编辑时基于拼音输入的补全原型
2019-04-26
从立创EDA,Gratipay看中文编程开发环境和推广运营的一个趋势
2019-04-26
中文代码之Django官方入门:建立模型
2019-04-26
Python实现推流直播
2019-04-26
你不得不了解的卷积神经网络发展史
2019-04-26
你不得不了解的机器学习知识
2019-04-26
你不得不了解的深度学习知识(一)
2019-04-26
你不得不了解的深度学习知识(二)
2019-04-26
AI算法之Encoder-Decoder 和 Seq2Seq
2019-04-26
AI算法之Attention机制
2019-04-26