netty客户端发送,服务端接收不到消息

3 篇文章 0 订阅

今天遇到使用netty做客户端的时候,发送数据,服务端接收不到,但是客户端不添加自己的hadler就可以,后来发现,由于的我的需要使用byte[]进行接收与发送,channelFuture.channel.writeAndFlush()需要使用ByteBuff发送,因此在outHandler接收的时候类型不对,outhandler接收的是inhandler发送过来的integer,造成了异常,因此需要在outhandler中先判断下类型,如果是int走我的模式,如果不是需要走其他模式

public class RTUClient {

    
    public void test(){
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.group(eventLoopGroup)
            .channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<Channel>() {

                protected void initChannel(Channel channel) throws Exception {
                    
                    channel.pipeline().addLast(new RTUOutHandler());
                    channel.pipeline().addLast(new RTUInHandler());
                    
//                    channel.pipeline().addLast(new StringEncoder());
//                    channel.pipeline().addLast(new StringDecoder());
                }
                
            });
            
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8086).sync();
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
            byteBuf.writeBytes("ETUNG:12345678901234511".getBytes(StandardCharsets.UTF_8));
            channelFuture.channel().writeAndFlush(byteBuf);
//            channelFuture.channel().writeAndFlush("ETUNG:123456789012345");
//            channelFuture.channel().writeAndFlush(Unpooled.buffer().writeByte(12));
            System.out.println("发送完毕");
            channelFuture.addListener(new ChannelFutureListener() {
                
                @Override
                public void operationComplete(ChannelFuture arg0) throws Exception {
                    System.out.println("发送成功");
                }
            });
//            channelFuture.channel().closeFuture().sync();
            System.out.println("发送完毕222");
        } catch (Exception e) {
            eventLoopGroup.shutdownGracefully();
        }
        
    }
    
    public static void main(String[] args) {
        RTUClient rtuClient = new RTUClient();
        rtuClient.test();
    }
}

public class RTUInHandler extends ChannelInboundHandlerAdapter{
    
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        
        ByteBuf byteBuf = (ByteBuf)msg;
        int length = byteBuf.readableBytes();
        byte[] receiveBytes = new byte[length];
        byteBuf.readBytes(receiveBytes);
        System.out.println("ByteUtil.toString(receiveBytes):"+ByteUtil.toString(receiveBytes));
        ReferenceCountUtil.release(msg);
        
        int step = 0;
        if(receiveBytes.length == 34){
            step = 0;
        }else{
            step = RTUUtil.bytesToInt(new byte[]{receiveBytes[3],receiveBytes[4],receiveBytes[5],receiveBytes[6]});
        }
        
        ctx.writeAndFlush(step);
                
//        ctx.writeAndFlush(msg);
    }

}


public class RTUOutHandler extends ChannelOutboundHandlerAdapter{
    
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        try{
            if(msg instanceof Integer){  //判断下类型,客户端首次发送是ByteBuffer类型
                int step = (int) msg;
                byte[] bytes = RTUUtil.intToBytes(step);
                ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
                ByteBuffer byteBuffer = ByteBuffer.allocate(7);
                byteBuffer.put((byte)0XAA);
                byteBuffer.put((byte)0XBB);
                byteBuffer.put((byte)0X00);
                for(int i=0;i<bytes.length;i++){
                    byteBuffer.put(bytes[i]);
                }
                byteBuffer.flip();
                byte[] bytes1 = byteBuffer.array();
                byte[] crc16 = CRC.swapCalcCrc16(bytes1);
                byteBuf.writeBytes(bytes1);
                byteBuf.writeBytes(crc16);
                super.write(ctx, byteBuf, promise);
            }else{
                super.write(ctx, msg, promise);
            }
            
        }catch(Exception ex){
            ex.printStackTrace();
        }
       
        
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值