在同步TCP服务器的情况下,当过一个客户端连接,我得到客户实例的引用:有没有什么办法可以让我在客户端连接到服务器端的服务器时创建ChannelPipeline的引用?
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 =新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引导=新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.
你看过示例服务器如echo服务器和HTTP snoop服务器吗? – trustin 2013-03-20 07:46:03
@TustinLee:我检查了EchoServer的例子,但是我找不到可以在哪里找到参考。我编辑了原始问题以更详细地解释我的问题。 – 2013-03-20 11:39:32