首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Netty 4,无法处理多个客户端

Netty 4,无法处理多个客户端
EN

Stack Overflow用户
提问于 2014-08-13 16:56:02
回答 1查看 1.2K关注 0票数 1

我决定找出我的笔记本可以从多个客户端处理多少味精/秒。我已经用下一种简单的方式将Echo客户机/服务器从示例中改变了: 1)在客户端,channelActive()方法中有无限循环。循环发送消息。2)在服务器端,我有处理传入消息的channelRead()方法

当我运行我的客户机2次(在两个间隔线程中)时,我希望看到服务器如何在两个线程中处理客户机。相反,服务器只处理一个客户端连接,有时根本不处理。我查看了我的客户机线程,发现它们不能退出ChannelOutboundBuffer.addFlush()方法中的while循环。我不明白我做错了什么。我使用netty 4.0.21

EchoClient.java

代码语言:javascript
运行
复制
    public static void main(String[] args) throws Exception {
    // Configure the client.
    final EventLoopGroup group = new NioEventLoopGroup();

    Runnable r = new Runnable() {
        @Override
        public void run() {
            try {

                Bootstrap b = new Bootstrap();
                b.group(group)
                        .channel(NioSocketChannel.class)
                        .option(ChannelOption.TCP_NODELAY, true)
                        .handler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast(new EchoClientHandler());
                            }
                        });

                // Start the client.
                ChannelFuture f = b.connect(HOST, PORT).sync();

                // Wait until the connection is closed.
                f.channel().closeFuture().sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // Shut down the event loop to terminate all threads.
                group.shutdownGracefully();
            }
        }
    };

    for (int i = 0; i < 2; i++) {
        Thread t = new Thread(r, i + " lalala");
        t.start();
    }
}

EchoClientHandler.java

代码语言:javascript
运行
复制
public class EchoClientHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelActive(ChannelHandlerContext ctx) {
    while (true) {
        ByteBuf time = ctx.alloc().buffer(4);
        time.writeInt(number);
        ctx.writeAndFlush(time);
        ctx.flush();
    }
}


@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    ctx.flush();
    ctx.fireChannelReadComplete();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    // Close the connection when an exception is raised.
    cause.printStackTrace();
    ctx.close();
    ctx.fireExceptionCaught(cause);
}

}

EchoServer.java

代码语言:javascript
运行
复制
public final class EchoServer {

    public static void main(String[] args) throws Exception {

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new EchoServerHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(8007).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

}

EchoServerHandler.java

代码语言:javascript
运行
复制
@Sharable

公共类EchoServerHandler扩展ChannelInboundHandlerAdapter {

代码语言:javascript
运行
复制
long max_msg = 10000;
long cur_msg = 0;
long startTime = System.nanoTime();
final int NANOS_IN_SEC = 1000000000;

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
   ReferenceCountUtil.release(msg);

    ++cur_msg;
    if (cur_msg == max_msg) {
        System.out.println("Throughput (msg/sec) : " + max_msg * NANOS_IN_SEC / (System.nanoTime() - startTime));
        cur_msg = 0;
        startTime = System.nanoTime();
    }
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    ctx.flush();
    ctx.fireChannelReadComplete();
}

}

EN

回答 1

Stack Overflow用户

发布于 2014-08-13 17:53:49

您的channelActive(.)中不能有无限循环。方法,因为这将阻止用于多个连接的EventLoop。这样,您将基本上阻止所有使用此EventLoop的通道的所有事件处理。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25291964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档