2015-06-25 2 views
1

Я новичок в Netty. Я использую Netty 4.0.28. Я имею в виду пример EchoServer, приведенный в книге «Netty In Action».Netty закрыть/отменить серверный канал

Все работает нормально, как за исключением. Клиент отправляет сообщение серверу на активном канале, сервер печатает полученное сообщение и отправляет обратно те же сообщения клиенту. Позже клиентский канал закрывается, но сервер все равно будет слушать новые подключения.

Я хочу также закрыть серверный канал и связать сервер с новым портом.

Что нужно сделать, чтобы закрыть серверный канал?

public class EchoServer { 

    private final int port; 

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

    public void start() throws Exception { 
     EventLoopGroup group = new NioEventLoopGroup(); 
     try { 
      ServerBootstrap b = new ServerBootstrap(); 
      b.group(group).channel(NioServerSocketChannel.class) 
        .localAddress(new InetSocketAddress(port)) 
        .childHandler(new ChannelInitializer<SocketChannel>() { 

         @Override 
         protected void initChannel(SocketChannel ch) 
           throws Exception { 
          ch.pipeline().addLast(new EchoServerHandler()); 

         } 
        }); 
      ChannelFuture f = b.bind().sync(); 
      System.out.println(EchoServer.class.getName() 
        + " started and listen on " + f.channel().localAddress()); 

      f.channel().closeFuture().sync(); 
     } finally { 
      group.shutdownGracefully(); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     if (args.length != 1) { 
      System.err.println("Usage: " + EchoServer.class.getSimpleName() 
        + " <port>"); 
      return; 
     } 
     int port = Integer.parseInt(args[0]); 
     new EchoServer(port).start(); 
    } 
} 


@Sharable 
public class EchoServerHandler extends ChannelInboundHandlerAdapter { 

    @Override 
    public void channelRead(ChannelHandlerContext ctx, Object msg) { 
     System.out.println("Server received: " + msg); 
     ctx.write(msg); 
    } 

    @Override 
    public void channelReadComplete(ChannelHandlerContext ctx) { 
     ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(
       ChannelFutureListener.CLOSE); 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
     cause.printStackTrace(); 
     ctx.close(); 
    } 
} 

public class EchoClient { 

    private final String host; 
    private final int port; 

    public EchoClient(String host, int port) { 
     this.host = host; 
     this.port = port; 
    } 

    public void start() throws Exception { 
     EventLoopGroup group = new NioEventLoopGroup(); 
     try { 
      Bootstrap b = new Bootstrap(); 
      b.group(group).channel(NioSocketChannel.class) 
        .remoteAddress(new InetSocketAddress(host, port)) 
        .handler(new ChannelInitializer<SocketChannel>() { 

         @Override 
         protected void initChannel(SocketChannel ch) 
           throws Exception { 
          ch.pipeline().addLast(new EchoClientHandler()); 

         } 
        }); 
      ChannelFuture f = b.connect().sync(); 
      f.channel().closeFuture().sync(); 
     } finally { 
      group.shutdownGracefully(); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     if (args.length != 2) { 
      System.err.println("Usage: " + EchoClient.class.getSimpleName() 
        + " <host> <port>"); 
      return; 
     } 

     final String host = args[0]; 
     final int port = Integer.parseInt(args[1]); 

     new EchoClient(host, port).start();; 
    } 
} 

@Sharable 
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { 

    @Override 
    public void channelActive(ChannelHandlerContext ctx) { 
     ctx.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", CharsetUtil.UTF_8)); 
    } 

    @Override 
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) 
      throws Exception { 
     System.out.println("Client received: "+ByteBufUtil.hexDump(msg.readBytes(msg.readableBytes()))); 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
     cause.printStackTrace(); 
     ctx.close(); 
    } 
} 

ответ

0

Вы должны различать канал «ребенок» (который вы можете получить от ctx.channel() в обработчике, прикрепленный к одной уникальной связи клиента, неявный refered по ctx) и каналу «родительского» (который вы можете получить от ctx.channel().parent()).

Закрытие «ребенка» не приведет к завершению f.channel() от вашего основного, поскольку этот является «родительским» (слушателем). Поэтому вы должны сделать что-то вроде

ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(
      ChannelFutureListener.CLOSE); 
ctx.closeFuture().addListener(new ChannelFutureListener() { 
     @Override 
     public void operationComplete(ChannelFuture future) { 
       ctx.channel().parent().close(); 
     } 
}); 
Смежные вопросы