netty开发遇到的问题

netty开发遇到的问题

背景

今天进行netty代码测试,所有开发完成后,出现客户端与服务器端通信失败,并且卡住。

原因

两边编码不一致导致

服务器端

服务器端编码配置

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //添加编解码
        socketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast(discardServerHandler);
    }

客户端

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //添加编解码
//        socketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
//        socketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast(discardServerHandler);
    }

当两边编码不一致时则会出现如下图现象:

2021-03-19 17:04:15.637  INFO 8604 --- [           main] com.netty.client.client.NettyClient      : 客户端成功....
2021-03-19 17:04:15.637  INFO 8604 --- [ntLoopGroup-2-1] c.n.client.handle.NettyClientHandler     : 客户端Active .....
2021-03-19 17:04:15.648  INFO 8604 --- [           main] com.netty.client.client.NettyClient      : 信息发送成功....

正常现象应该是

2021-03-19 17:35:18.958  INFO 7288 --- [ntLoopGroup-2-1] c.n.client.handle.NettyClientHandler     : 客户端Active .....
2021-03-19 17:35:18.958  INFO 7288 --- [           main] com.netty.client.client.NettyClient      : 客户端成功....
2021-03-19 17:35:19.030  INFO 7288 --- [           main] com.netty.client.client.NettyClient      : 信息发送成功....
2021-03-19 17:35:19.385  INFO 7288 --- [ntLoopGroup-2-1] c.n.client.handle.NettyClientHandler     : 客户端收到消息: ceshi ok

当服务器端不设置编码时
通信同样会出现上述问题。同时telnet localhost 10001服务器端会出现问题

2021-03-19 17:04:31.490  INFO 9956 --- [ntLoopGroup-3-2] c.n.server.handle.DiscardServerHandler   : Channel active......
2021-03-19 17:04:33.238  INFO 9956 --- [ntLoopGroup-3-2] c.n.server.handle.DiscardServerHandler   : PooledUnsafeDirectByteBuf(ridx: 0, widx: 1, cap: 2048)
2021-03-19 17:04:35.827  INFO 9956 --- [ntLoopGroup-3-2] c.n.server.handle.DiscardServerHandler   : PooledUnsafeDirectByteBuf(ridx: 0, widx: 1, cap: 2048)

该问题待查证,PooledUnsafeDirectByteBuf是什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值