简体   繁体   中英

I write an application with netty, send message from client to server, but server cannot receive the message

I write a application with netty, the client send string message to server in channelActive method, but the server do not receive the message. I do not know the reason.

Server code:

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();
}
}

I print the message from client in server handler's method channelRead.

following is client code:

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

}
}

I sent message from client where channel active, but the server do not receive message, the string "message from client"

Thats because you use a String when calling writeAndFlush(...) . The returned ChannelFuture should tell you the message type is not supported. If you want to use a String you will need to put the StringEncoder on your ChannelPipeline , otherwise you will need to write ByteBuf instances only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM