Question

I'm having trouble figuring out what is wrong with the a simple Netty chat program I am working with.

I have tried debugging through the program but it hasn't been able to show me where the problem even lies.

Through debugging it seems like the client is connecting to the server, but the write function doesn't seem to be working.

I followed the tutorial here. I have my source code here on GitHub.

Here is the Client classes.

public void run() throws Exception {
    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");
        }

    } finally {
        group.shutdownGracefully();
    }
}

public class ChatClientInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new ChatClientHandler());
    }
}

public class ChatClientHandler extends SimpleChannelInboundHandler<String>{


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    System.out.println(msg);
    }
}

And here are the Server classes

public void run() throws Exception{
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try{
        ServerBootstrap bootstrap = new ServerBootstrap()
            .group(bossGroup,workerGroup)
            .channel(NioServerSocketChannel.class)
            .childHandler(new ChatServerInitializer());

        bootstrap.bind(port).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

public class ChatServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

    ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    pipeline.addLast("handler", new ChatServerHandler());
    }

}

public class ChatServerHandler extends SimpleChannelInboundHandler<String>{

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

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception{
    Channel incoming = ctx.channel();

    channels.add(ctx.channel());

        for(Channel channel : channels){
        channel.write("[SERVER] - " + incoming.remoteAddress() + "has joined\n");
        }
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    Channel leaving = ctx.channel();


        for(Channel channel : channels){
        channel.write("[SERVER] - " + leaving.remoteAddress() + "has left\n");
        }
    channels.remove(ctx.channel());
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg)
        throws Exception {
    Channel incoming = ctx.channel();
    System.out.println(msg);

        for(Channel channel : channels){
        channel.write("[" + incoming.remoteAddress() + "]" + msg +"\n");    
        }   
    }

}
Was it helpful?

Solution

You need to replace channel.write(...) with channel.writeAndFlush(...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top