netty拆包粘包问题处理
来源:互联网 发布:申请淘宝店铺多少钱 编辑:程序博客网 时间:2024/06/05 03:34
public classServerHandler extends ChannelHandlerAdapter {
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String request = (String)msg; System.out.println("Server:"+request); String response = "服务器响应数据:"+msg+"$_"; ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes())); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); }}
public class Server { public static void main(String[] args) throws Exception { //第一个线程组 接受client端 EventLoopGroup bossGroup = new NioEventLoopGroup(); //第二个线程组 实际业务处理 EventLoopGroup workerGroup = new NioEventLoopGroup(); //3 创建一个辅助类Bootstrap,就是对我们的Server进行一系列的配置 ServerBootstrap b = new ServerBootstrap(); //把两个工作线程组加进来 b.group(bossGroup, workerGroup) //指定使用NioServerSocketChannel .channel(NioServerSocketChannel.class) //一定要使用 childHandler 去绑定具体的 事件处理器 .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf)); sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ServerHandler());//这里可以加多个处理器 } }); //设置tcp的缓冲区 // .option(ChannelOption.SO_BACKLOG, 128) //保持连接 // .option(ChannelOption.SO_KEEPALIVE, true); //绑定指定端口 进行监听 ChannelFuture f = b.bind(8920).sync(); Thread.sleep(1000000); bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); }}
public class ClientHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try{ String request = (String)msg; System.out.println("client:"+request); } finally {//没有调用write方法 需要手动释放 ReferenceCountUtil.release(msg); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); }}
public class Client { public static void main(String[] args) throws Exception { EventLoopGroup workGroup = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(workGroup) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf)); sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ClientHandler()); } }); ChannelFuture f = b.connect("127.0.0.1",8920).sync();//buf f.channel().write(Unpooled.copiedBuffer(Unpooled.wrappedBuffer("bbb1$_".getBytes()))); f.channel().write(Unpooled.copiedBuffer(Unpooled.wrappedBuffer("bbb2$_".getBytes()))); f.channel().write(Unpooled.copiedBuffer(Unpooled.wrappedBuffer("bbb3$_".getBytes()))); f.channel().flush();// f.channel().flush(); f.channel().closeFuture().sync(); workGroup.shutdownGracefully(); }}
阅读全文
0 0
- netty拆包粘包问题处理
- netty 粘包问题处理
- Netty长连接的事件处理顺序问题
- Netty长连接的事件处理顺序问题
- Netty长连接的事件处理顺序问题
- 实例:Netty 处理 TCP协议数据分包问题
- 实例:Netty 处理 TCP协议数据分包问题
- Netty长连接的事件处理顺序问题
- netty处理粘包问题——1
- netty处理粘包问题——2
- netty中的粘包和拆包问题处理
- java netty使用DelimiterBasedFrameDecoder处理tcp粘包问题
- Netty学习笔记13——一些问题记录处理
- Netty学习(二)—拆包粘包问题
- Netty多线程处理机制
- Netty多线程处理机制
- Netty多线程处理机制
- netty字节处理IndexOutOfBoundsException
- HDU6112今夕何夕(Java写法)
- linux apache
- Leetcode557. Reverse Words in a String III
- python可变参数和可改变对象
- application/x-www-form-urlencoded(AFNetwork 方式实现)
- netty拆包粘包问题处理
- 编程题
- PS 的基本使用
- 线程基础函数
- 线段树模板
- 水池数目(bfs模板题)
- 关注国情民情——华北水利水电大学“情艺”国情社情调研团
- C语言(一)--开发步骤,注释,gcc
- ArrayList(二)线程不安全详解