0

如果客户端连接时使用同步 TCP 服务器,我会得到客户端示例的参考:

 int serverPortNum = 9000;
socket while(true) {
     ServerSocket connectionSocket = new ServerSocket(serverPortNum);
     Socket dataSocket = connectionSocket.accept( );
     // pass this reference(dataSocket ) to other part of program to read or write depending on app logic
}

现在我想使用使用 Netty 的异步 TCP 服务器,所以有什么方法可以获取连接新客户端时创建的 ChannelPipeline 或 ChannelHandler 的引用。

在客户端,我可以轻松做到:示例代码:NioClientSocketChannelFactory ncscf = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ClientBootstrap clientBootstrap = new ClientBootstrap(ncscf);

    final DummyHandler  dummy = new DummyHandler();
    clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() { 
        @Override
        public ChannelPipeline getPipeline() throws Exception { 
            return Channels.pipeline(dummy); 
        } 
    }); 

    InetAddress inetaddress = InetAddress.getByName(host);
    ChannelFuturecf=clientBootstrap.connect(newInetSocketAddress(inetaddress,port));                                                             

所以每次我创建一个新客户端时,我都有新的 DummyHandler 参考

在服务器端:示例代码:ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(new DummyServerHandler());
        }
    });


    bootstrap.bind(new InetSocketAddress(port));
   So when client request connection new DummyServerHandler object is created but i cannot get reference of this.  
4

1 回答 1

0

I may missunderstood the question but wouldn't this work.

Channel.getpipeline()
于 2013-03-23T15:40:21.373 回答