简体   繁体   English

channelRead0 没有被调用 Netty

[英]channelRead0 not getting called Netty

I have this server-client application that does not work.我有这个不起作用的服务器客户端应用程序。 This is the code: ChatServerHandler这是代码: ChatServerHandler

public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf buf) {
    Channel incoming = channelHandlerContext.channel();
    for (Channel channel : channels) {
        if (channel != incoming){
            System.out.println(buf);
            channel.writeAndFlush("[" + incoming.remoteAddress() + "]" + buf + "\r\n");
        }
    }
    buf.release();
}

@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    Channel incoming = ctx.channel();
    for (Channel channel : channels) {
        channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " has joined" + "\r\n");
    }
    channels.add(incoming);
}

@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
    Channel incoming = ctx.channel();
    for (Channel channel : channels) {
        channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " has left" + "\r\n");
    }
    channels.remove(incoming);
}

ChatServerInitializer聊天服务器初始化器

@Override
protected void initChannel(SocketChannel channel) {
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringDecoder());
    pipeline.addLast("handler", new ChatServerHandler());
}

ChatServer聊天服务器

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

public void run() throws Exception{
    EventLoopGroup boosGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap()
                .group(boosGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChatServerInitializer());
        bootstrap.bind(port).sync().channel().closeFuture().sync();
    }finally {
        boosGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

ChatClientHandler聊天客户端处理程序

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) {
    System.out.println(s);
}

ChatClientInitializer ChatClientInitializer

@Override
protected void initChannel(SocketChannel channel) {
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    pipeline.addLast("handler", new ChatClientHandler());
}

ChatClient聊天客户端

public void run() {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap  = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChatClientInitializer());
        Channel channel = bootstrap.connect(host, port).sync().channel();
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            channel.write(in.readLine() + "\r\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}

Just to mention, every thing else works fine, the messages are just not received.顺便提一下,其他一切都很好,只是没有收到消息。 Trying to print something at channelRead using println will not work.尝试使用printlnchannelRead打印一些东西是行不通的。 That's how I know it is not getting called.这就是我知道它没有被调用的方式。 Also, sorry if I shouldn't have posted the entire code here but I'm just out of ideas.另外,对不起,如果我不应该在这里发布整个代码,但我只是没有想法。

You need to use writeAndFlush(in.readLine() + "\r\n");您需要使用writeAndFlush(in.readLine() + "\r\n"); as otherwise it is not "flushed" to the underlying socket.否则它不会“刷新”到底层套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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