首页 > 解决方案 > 我用netty编写了一个应用程序,从客户端向服务器发送消息,但服务器无法接收到消息

问题描述

我用netty写了一个应用程序,客户端通过channelActive方法向服务器发送字符串消息,但是服务器没有收到消息。我不知道原因。

服务器代码:

public static void main(String[] args) throws InterruptedException {
    NioEventLoopGroup boss = new NioEventLoopGroup();
    NioEventLoopGroup worker = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();

        bootstrap.group(boss,worker);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new NettyServerHandler());
            }
        });
        bootstrap.option(ChannelOption.SO_BACKLOG,1024);
        bootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);

        ChannelFuture sync = bootstrap.bind(8080).sync();

        System.out.println("server start");

        sync.channel().closeFuture().sync();

        System.out.println("server end");
    } finally {
        worker.shutdownGracefully();
        boss.shutdownGracefully();
    }
}

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("channelRead");

    super.channelRead(ctx, msg);
    System.out.println(((ByteBuf) msg).toString(Charset.defaultCharset()));

}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channelActive");
    super.channelActive(ctx);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    System.out.println("exceptionCaught");
    super.exceptionCaught(ctx, cause);

    cause.printStackTrace();
    ctx.close();
}
}

我在服务器处理程序的方法 channelRead 中打印来自客户端的消息。

以下是客户端代码:

public static void main(String[] args) throws InterruptedException {
    NioEventLoopGroup worker = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(worker);
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.handler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new NettyClientHandler());
            }
        });

        ChannelFuture future = bootstrap.connect("localhost", 8080).sync();

        future.channel().closeFuture().sync();
    }finally {
        worker.shutdownGracefully();
    }
}

public class NettyClientHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channelActive");
    super.channelActive(ctx);
    ctx.writeAndFlush("message from client");
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("channelRead");
    super.channelRead(ctx, msg);

}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    System.out.println("exceptionCaught");
    super.exceptionCaught(ctx, cause);

    cause.printStackTrace();
    ctx.close();

}
}

我从通道活动的客户端发送消息,但服务器没有收到消息,字符串“来自客户端的消息”

标签: javanetty

解决方案


那是因为您String在调用时使用了 a writeAndFlush(...)。返回的ChannelFuture应该告诉您消息类型不受支持。如果你想使用 aString你需要把StringEncoder你的ChannelPipeline,否则你只需要编写ByteBuf实例。


推荐阅读