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.awaitUninterruptively().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 に答える