在同步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的引用。
在客户端我可以很容易地做到这一点:示例代码: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 = new ServerBootstrap(新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.
我可能误解了这个问题,但是这样不行。
Channel.getpipeline()