2014-04-07 102 views
0

我無法弄清楚我正在使用的簡單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");  
     } 
    } 

} 
+0

具有u解決問題了嗎?我遇到了同樣的問題 – Xiaokun

回答

1

您需要更換與channel.writeAndFlush(...)channel.write(...)