2014-02-24 195 views
0

我想監聽兩個不同端口netty4:如何在多個端口上監聽一個java進程

連接我開始在Java主方法2線程,每個線程綁定netty4端口,但不能 監聽成功! 這是我的代碼,端口3333是好的,但1234也不行,它看起來像3333阻止!

public class ObjectServer 
    { 
private static final Logger logger = LoggerFactory.getLogger(ObjectServer.class); 

private String ip; 
private int port; 

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

public void run(final ChannelInboundHandlerAdapter handler) throws Exception 
{ 
    EventLoopGroup bossGroup = new NioEventLoopGroup(); 
    EventLoopGroup workerGroup = new NioEventLoopGroup(); 
    try 
    { 
     ServerBootstrap server = new ServerBootstrap(); 
     server.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() 
     { 
      @Override 
      public void initChannel(SocketChannel ch) throws Exception 
      { 
       ch.pipeline().addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), handler); 
      } 
     }); 
     server.bind(port).sync().channel().closeFuture().sync(); 
    } 
    catch (Exception e) 
    { 
     logger.error("開啓監聽失敗!端口[" + port + "]", e); 
     throw e; 
    } 
    finally 
    { 
     bossGroup.shutdownGracefully(); 
     workerGroup.shutdownGracefully(); 
    } 
} 

}

public class SocketServer 
{ 
private static final Logger logger = LoggerFactory.getLogger(SocketServer.class); 
private static final StringDecoder DECODER = new StringDecoder(); 
private static final StringEncoder ENCODER = new StringEncoder(); 
private int port; 

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

public void run(final ChannelInboundHandlerAdapter handler) throws Exception 
{ 
    EventLoopGroup bossGroup = new NioEventLoopGroup(); 
    EventLoopGroup workerGroup = new NioEventLoopGroup(); 
    try 
    { 
     ServerBootstrap b = new ServerBootstrap(); 
     b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() 
     { 
      @Override 
      public void initChannel(SocketChannel ch) throws Exception 
      { 
       ChannelPipeline pipeline = ch.pipeline(); 
       // Add the text line codec combination first, 
       pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); 
       // the encoder and decoder are static as these are 
       // sharable 
       pipeline.addLast("encoder", ENCODER); 
       pipeline.addLast("decoder", DECODER); 
       // and then business logic. 
       pipeline.addLast("handler", handler); 
      } 
     }); 
     b.bind(port).sync().channel().closeFuture().sync(); 
    } 
    catch (Exception e) 
    { 
     logger.error("開啓監聽失敗!端口[" + port + "]", e); 
     throw e; 
    } 
    finally 
    { 
     bossGroup.shutdownGracefully(); 
     workerGroup.shutdownGracefully(); 
    } 
} 

}

public class Test 
     { 
    public static void main(String[] args) throws Exception 
    { 
     Thread1 thread1 = new Thread1(); 
     Thread2 thread2 = new Thread2(); 
     thread2.start(); 
     thread1.start(); 
     new SocketClient("192.168.16.52", 3333).run(new TestHandler4("test4")); 
     new ObjectClient("192.168.16.52", 1234).run(new TestHandler3("test3")); 
    } 

    @Sharable 
    static class TestHandler1 extends ChannelInboundHandlerAdapter 
    { 
     @Override 
     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception 
     { 
      System.out.println("1234" + msg); 
     } 

    } 

    static class Thread1 extends Thread 
    { 
     @Override 
     public void run() 
     { 
      try 
      { 
       new ObjectServer(1234).run(new TestHandler1()); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 

    static class Thread2 extends Thread 
    { 
     @Override 
     public void run() 
     { 
      try 
      { 
       new SocketServer(3333).run(new TestHandler2()); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 

    @Sharable 
    static class TestHandler2 extends SimpleChannelInboundHandler<String> 
    { 
     @Override 
     public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception 
     { 
      System.out.println("3333" + msg); 
     } 

     @Override 
     public void channelActive(ChannelHandlerContext ctx) throws Exception 
     { 
      System.out.println("sssssssssssssssss"); 
     } 
    } 

    @Sharable 
    static class TestHandler3 extends ChannelInboundHandlerAdapter 
    { 
     private String msg; 

     public TestHandler3(String msg) 
     { 
      this.msg = msg; 
     } 

     @Override 
     public void channelActive(ChannelHandlerContext ctx) throws Exception 
     { 
      ctx.writeAndFlush(msg); 
     } 
    } 

    @Sharable 
    static class TestHandler4 extends SimpleChannelInboundHandler<String> 
    { 
     private String msg; 

     public TestHandler4(String msg) 
     { 
      this.msg = msg; 
     } 

     @Override 
     public void channelActive(ChannelHandlerContext ctx) throws Exception 
     { 
      ctx.writeAndFlush(msg); 
     } 

     @Override 
     protected void channelRead0(ChannelHandlerContext arg0, String arg1)throws  Exception 
     { 

     } 
    } 
} 
+1

需要你的代碼。 –

+0

感謝!我的代碼被上傳! – user3346346

回答

1

在你run()實現,你這樣做:

server.bind(port).sync().channel().closeFuture().sync(); 

..這將阻止,直到服務器關閉套接字。因爲你不關閉服務器套接字,它永遠不會回來。因此,只有第一個服務器套接字將被綁定。

你可能想只是綁定並返回,而不是等待套接字關閉服務器是什麼。

+0

我在不同的線程中綁定了不同的端口,爲什麼在第一個線程被阻塞時調用方法,會影響到第二個線程。我只是想要一個使用兩個線程來監聽不同端口的主要方法。如何讓兩個端口都在監聽,受阻。你可以幫我嗎?非常感謝你!! – user3346346