1

我是否可以在客户端通道处理程序中动态构建客户端客户端/服务器?如果有可能,你怎么能做到?(我已经尝试过了,但是在使用 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

4

1 回答 1

0

代理示例实际上是这样做的,它在处理程序中创建一个新的客户端连接:

https://github.com/netty/netty/blob/b03b11a24860a1d636744c989dad50d223ffc6bc/src/main/java/org/jboss/netty/example/proxy/HexDumpProxyInboundHandler.java

于 2012-05-07T00:48:55.513 回答