我用netty编写了一个应用程序,将消息从客户端发送到服务器,但是服务器无法接收消息

时间:2019-01-14 06:21:38

标签: java 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();

}
}

我从通道处于活动状态的客户端发送了消息,但是服务器未收到消息,字符串“来自客户端的消息”

1 个答案:

答案 0 :(得分:0)

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