如果是客户端连接的同步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( 新的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.
答案 0 :(得分:0)
我可能会错过这个问题,但不会这样做。
Channel.getpipeline()