【问题标题】:I write an application with netty, send message from client to server, but server cannot receive the message我用netty编写了一个应用程序,从客户端向服务器发送消息,但服务器无法接收到消息
【发布时间】:2019-06-08 03:36:42
【问题描述】:

我用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();

}
}

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

【问题讨论】:

    标签: java netty


    【解决方案1】:

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

    【讨论】:

    • 我尝试将 StringEncoder 添加到管道并编写一个 ByteBuff,一切正常。太好了,谢谢你变化很大!!!
    猜你喜欢
    • 1970-01-01
    • 2017-03-08
    • 2018-05-20
    • 2011-05-12
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多