Netty解决粘包问题(2) 定长填充
来源:互联网 发布:韩氏父子 圆号网络 编辑:程序博客网 时间:2024/05/21 10:22
第二种解决粘包的方案,就是每次发送固定长度的数据包,如果长度不够,那就用空字符串填充
import java.nio.ByteBuffer;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.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;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.FixedLengthFrameDecoder;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;public class Server { public static void main(String[] args) throws Exception{ //1 创建2个线程,一个是负责接收客户端的连接。一个是负责进行数据传输的 EventLoopGroup pGroup = new NioEventLoopGroup(); EventLoopGroup cGroup = new NioEventLoopGroup(); //2 创建服务器辅助类 ServerBootstrap b = new ServerBootstrap(); b.group(pGroup, cGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .option(ChannelOption.SO_SNDBUF, 32*1024) .option(ChannelOption.SO_RCVBUF, 32*1024) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { //设置定长字符串接收 sc.pipeline().addLast(new FixedLengthFrameDecoder(5)); //设置字符串形式的解码 sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ServerHandler()); } }); //4 绑定连接 ChannelFuture cf = b.bind(8765).sync(); //等待服务器监听端口关闭 cf.channel().closeFuture().sync(); pGroup.shutdownGracefully(); cGroup.shutdownGracefully(); }}
import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;public class ServerHandler extends ChannelHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println(" server channel active... "); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String request = (String)msg; System.out.println("Server :" + msg); String response = request ; ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes())); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable t) throws Exception { }}
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.FixedLengthFrameDecoder;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;public class Client { public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { sc.pipeline().addLast(new FixedLengthFrameDecoder(5)); sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ClientHandler()); } }); ChannelFuture cf = b.connect("127.0.0.1", 8765).sync(); cf.channel().writeAndFlush(Unpooled.wrappedBuffer("aaaaabbbbb".getBytes())); cf.channel().writeAndFlush(Unpooled.copiedBuffer("ccccccc".getBytes())); //等待客户端端口关闭 cf.channel().closeFuture().sync(); group.shutdownGracefully(); }}
import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;public class ClientHandler extends ChannelHandlerAdapter{ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("client channel active... "); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String response = (String)msg; System.out.println("Client: " + response); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { }}
运行结果
//服务器端server channel active... Server :aaaaaServer :bbbbbServer :ccccc
//客户端client channel active... Client: aaaaaClient: bbbbbClient: ccccc
0 0
- Netty解决粘包问题(2) 定长填充
- 发送定长宝解决网络粘包问题
- Netty使用LineBasedFrameDecoder解决粘包问题
- Netty初探-解决TCP粘包/拆包问题
- Netty解决粘包问题(1) 自定义分隔符
- 网络编程复习(八):Netty解决拆包粘包问题--定长方式
- 4、netty之解决tcp粘包问题(netty学习笔记)
- Netty 粘包、断包解决
- Netty 粘包、拆包解决示例
- Netty解决半包(TCP粘包/拆包导致)读写问题
- Netty解决半包(TCP粘包/拆包导致)读写问题
- Netty解决半包(TCP粘包/拆包导致)读写问题
- Netty解决TCP粘包/拆包导致的半包读写问题
- netty 粘包问题处理
- netty学习笔记(2)_tcp拆包/粘包问题
- netty解决TCP网络传输中的拆包与粘包问题
- netty的粘包 解包问题
- Netty之解决TCP粘包拆包(设置定长消息)
- Android Camera 通过V4L2与kernel driver的完整交互过程
- 2016/8/31 求逆序数+约瑟夫杀人环问题
- MySQL 5.7 64位 Windows安装指南
- Po学校——考试一
- leetcode 382 Linked List Random Node C++
- Netty解决粘包问题(2) 定长填充
- 详细“企业库”编写流程
- MkDocs项目文档生成器(一)
- java字符输入流与输出流
- 深度分析如何在Hadoop中控制Map的数量
- 01背包 完全背包 多重背包
- android中的表格,固定第一行和第一列,使用fragment显示
- 斐波那契数列
- Android内存泄漏分析及调试