我是否有可能在客户端通道处理程序中动态构建客户端客户端/服务器?如果有可能,你怎么能实现它? (我试过这个但是在使用bootstrap.connect连接时构建子客户端失败了,另一方面,我已经成功构建了一个与某个端口绑定的子服务器,不知道为什么)
是否有更好的方法来实现上述功能,使客户端与其他服务器/客户端进行动态通信,而不是首先连接的服务器,但保持连接活动?
增加: 一些代码如下:
public class FileClientHandler extends SimpleChannelUpstreamHandler {
.....
public void getFile(final String filename, final String md5){
// Configure the client.
ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
// Configure the pipeline factory.
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline( new FileClientGetFileHandler(filename,md5) );
}
});
// Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(Ip, Port));
// Wait until the connection attempt succeeds or fails.
future.awaitUninterruptibly().getChannel();
if (!future.isSuccess()) {
System.out.print("Connect Failure!\n");
future.getCause().printStackTrace();
bootstrap.releaseExternalResources();
return;
}
// Wait until the connection is closed or the connection attempt fails.
future.getChannel().getCloseFuture().awaitUninterruptibly();
// Shut down thread pools to exit.
bootstrap.releaseExternalResources();
}
......
}
它失败了 future.awaitUninterruptibly()。getChannel(); ,如果删除它,仍然在连接失败:!future.isSuccess()== true
答案 0 :(得分:0)