io.netty.buffer.SimpleLeakAwareByteBuf cannot be cast to java.lang.String

在跟着学习 netty 的粘包和毡包处理时,使用netty自带编码解码类时遇到的一个小问题,附解决方式.

client端


import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
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.string.StringDecoder;
import io.netty.util.CharsetUtil;

/**
 * @author lin 2022/8/9 22:59
 */
public class Client {


    public void startClient() {
        NioEventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();

        try {
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {

                            ByteBuf tag = Unpooled.copiedBuffer("@_".getBytes());
                            // netty 自带的编码  分隔符解码器     链式结构  先读成数组 再解析成字符串
                            socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,tag));
                            //责任分发出去
                            socketChannel.pipeline().addLast(new ClientHandler());
                            socketChannel.pipeline().addLast(new StringDecoder());
                        }
                    });
            ChannelFuture future = bootstrap.connect("localhost", 8080).sync();
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }

    }

    private static class ClientHandler extends ChannelHandlerAdapter  {
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            new Thread(() -> {
                // 模拟粘包
                for (int i = 0; i < 100; i++) {
                    ByteBuf send = Unpooled.copiedBuffer(("from client"+i+"@_").getBytes(CharsetUtil.UTF_8));
                    ctx.writeAndFlush(send);
                }
            }).start();
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.startClient();
    }
}

Server 端

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
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.string.StringDecoder;

import java.nio.charset.Charset;

/**
 * @author lin 2022/8/9 22:45
 */
public class Server {


    public void startServer() throws Exception {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();


        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(
                            //SocketChannel 的管理
                            new ChannelInitializer() {
                                @Override
                                protected void initChannel(SocketChannel socketChannel) throws Exception {
                                    ByteBuf tag = Unpooled.copiedBuffer("@_".getBytes());
                                    socketChannel.pipeline().addLast(new Handler());
                                    // netty 自带的编码  分隔符解码器
                                    socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,tag));
                                    socketChannel.pipeline().addLast(new StringDecoder());
                                }
                            });

            ChannelFuture channelFuture = bootstrap.bind(8080).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            boss.shutdownGracefully();

            worker.shutdownGracefully();
        }
    }

    private static class Handler extends ChannelHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    
                String text= (String) msg;
                System.out.println("get once message here[[[[" +  text + "]]]]");
            

        }
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server();
        server.startServer();

    }

}

抛出转换异常

java.lang.ClassCastException: io.netty.buffer.SimpleLeakAwareByteBuf cannot be cast to java.lang.String

解决方式 修改服务端接受消息转换的类型

  String text= (String) msg;
                System.out.println("get once message here[[[[" +  text + "]]]]");

// 变更为
       if (msg instanceof ByteBuf) {
                ByteBuf packet = (ByteBuf) msg;
                System.out.println("get once message here[[[[" +  packet.toString(Charset.defaultCharset()) + "]]]]");
            }
   

client 端发送的消息 为bytebuf,先转换成buf 再进行字符串处理

你可能感兴趣的:(io.netty.buffer.SimpleLeakAwareByteBuf cannot be cast to java.lang.String)