【问题标题】:Message is not received by client in Netty when there are more number of messages sent to Server当发送到服务器的消息数量更多时,Netty 中的客户端未收到消息
【发布时间】:2017-05-22 15:36:07
【问题描述】:

我使用 JAVA 中的 Netty 包实现了一个非常基本的 EchoClient 和 EchoServer。我对 Netty 非常陌生。下面是我的 client 、 clientHandler 、 server 和 serverHandler 的代码。

我的客户

public class EchoClient {
    private final String host;
    private final int port;

    public EchoClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        new EchoClient("127.0.0.1", 11235).start();
    }

    public void start() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class)
                    .remoteAddress(new InetSocketAddress(host, port))
                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new EchoClientHandler());
                        }
                    });
            ChannelFuture future = b.connect().sync();
            future.channel().closeFuture().sync();

        } finally {
            group.shutdownGracefully().sync();
        }

    }
}

我的客户处理程序

public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf>{

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        System.out.println("Connected");
        int i = 0;
        while (i < 1000) {
            ctx.writeAndFlush(Unpooled.copiedBuffer("Netty MAY rock!\n", CharsetUtil.UTF_8));
            i++;
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        System.out.println(
                "Client received: " + in.toString(CharsetUtil.UTF_8));

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
        cause.printStackTrace();
        ctx.close();
    }
}

我的服务器

public class EchoServer {

    private final int port;

    public EchoServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        new EchoServer(11235).start();
    }

    public void start() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            System.out.println("New client connected: " + ch.localAddress());
                            ch.pipeline().addLast(new EchoServerHandler());
                        }
                    });
            ChannelFuture f = b.bind().sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }
}

我的服务器处理程序

public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("Server received: " + n.toString(CharsetUtil.UTF_8));
        ctx.write(in);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
                .addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

现在的问题是:当我在 ClientHandler 中将 while 循环编写为 i

【问题讨论】:

  • 不要对未引用的文本使用引号格式。
  • 好的,谢谢。我会从下一个问题改进

标签: java sockets netty


【解决方案1】:

您在第一次阅读后立即关闭您的服务器频道。请删除您的EchoServerHandler 类的channelReadComplete 中的.addListener(ChannelFutureListener.CLOSE)

【讨论】:

  • 你能推荐我任何关于 Netty 的书吗?或者任何可以用一些好的例子清楚地解释 netty 的东西。
  • 嗯,听说Netty in action是一本不错的书,但我没看。而且official manual也很不错。
  • 非常感谢。我已经读过那本书了。它只涵盖基础知识。有没有深入探讨主题的书?
  • 不,不幸的是我不知道更详细的书。
猜你喜欢
  • 2017-03-08
  • 2012-12-05
  • 2020-12-25
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
相关资源
最近更新 更多