聊天服务器的网络问题

时间:2014-04-07 02:25:30

标签: java netty

我无法弄清楚我正在使用的简单Netty聊天程序有什么问题。

我已尝试通过该程序进行调试,但它无法向我展示问题所在。

通过调试,似乎客户端连接到服务器,但写入功能似乎不起作用。

我按照教程here进行了操作。 我在GitHub上有我的源代码here

这是客户端类。

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);
    }
}

这是服务器类

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");    
        }   
    }

}

1 个答案:

答案 0 :(得分:1)

您需要将channel.write(...)替换为channel.writeAndFlush(...)