zoukankan      html  css  js  c++  java
  • Netty(7-2)传List

    ObjectEchoServer

    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new ObjectEncoder());
                        p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        p.addLast(new ObjectEchoServerHandler());
                    }

    ObjectEchoServerHandler

    public class ObjectEchoServerHandler extends ChannelInboundHandlerAdapter {
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ctx.write(msg);
        }
    
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    
    }

    ObjectEchoClient

    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new ObjectEncoder());
                        p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        p.addLast(new ObjectEchoClientHandler());
                    }

    ObjectEchoClientHandler

    public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {
        private final List<Integer> firstMessage;
        
        public ObjectEchoClientHandler() {//每个channel(connection)都会new该handler
            firstMessage = new ArrayList<Integer>(ObjectEchoClient.SIZE);
            for (int i = 0; i < ObjectEchoClient.SIZE; i++) {
                firstMessage.add(Integer.valueOf(i));
            }
        }
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.writeAndFlush(firstMessage);
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            //Echo back the received object to the server.
            ctx.write(msg);
        }
    
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    
    }
  • 相关阅读:
    IDE警告信息不应该被忽略
    C#委托使用:多播 ,向委托注册多个方法
    C# random生成随机数全部一样
    使用dynamic动态设置属性值与反射设置属性值性能对比
    git基本使用
    sql server多数据库查询 远程数据库查询
    C# mvc统一通道使用过滤器
    拼凑json的实例
    java常考小程序
    几个触发器的实例
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/9749936.html
Copyright © 2011-2022 走看看