2017-05-19 8 views
0

Nettyに構築しているサーバーから別のクライアントに接続しようとしています。 http://netty.io/4.1/xref/io/netty/example/proxy/package-summary.htmlchannelhandlerのnettyでクライアントに接続

だからChannelInboundHandlerAdapterの私のサブクラスでは、私はこの

ctx.pipeline().addLast(new EchoTestHandler("localhost", 3030)); 

マイEchoTestHandlerがどのように見えるやろう:私はここに、プロキシの例を見て

public class EchoTestHandler extends ChannelInboundHandlerAdapter { 

    private final String host; 
    private final int port; 
    private Channel outboundChannel; 

    public EchoTestHandler(String host, int port) { 
     System.out.println("constructor echo test handler"); 
     this.host = host; 
     this.port = port; 
    } 

    @Override 
    public void channelActive(ChannelHandlerContext ctx) { 
     System.out.println("channel test handler"); 

     final Channel inboundChannel = ctx.channel(); 

     // start the connection attempt 
     Bootstrap bootstrap = new Bootstrap(); 
     bootstrap.group(inboundChannel.eventLoop()) 
       .channel(ctx.channel().getClass()) 
       .handler(new CryptoServerHandler(inboundChannel)); 
     ChannelFuture future = bootstrap.connect(host, port); 
     outboundChannel = future.channel(); 
     future.addListener(new ChannelFutureListener() { 
      @Override 
      public void operationComplete(ChannelFuture channelFuture) { 
       if (channelFuture.isSuccess()) { 
        // connection complete, start to read first data 
        inboundChannel.read(); 
       } else { 
        // close the connection if connection attempt has failed 
        inboundChannel.close(); 
       } 
      } 
     }); 
    } 
} 

コンストラクタが呼び出されますが、まだ何も接続していないので、channelActiveは決して呼び出されません。あなたはchannelActiveコールを実行するためにプロキシサーバーに何かを接続する必要があり

public class EchoServerInitializer extends ChannelInitializer<SocketChannel> { 

    private final String host; 
    private final int port; 

    public EchoServerInitializer(String host, int port) { 
     System.out.println("constructor EchoServerInitializer"); 
     this.host = host; 
     this.port = port; 
    } 

    @Override 
    public void initChannel(SocketChannel ch) { 
     System.out.println("EchoServerInitializer initChannel"); 
     ch.pipeline().addLast(
       new LoggingHandler(LogLevel.INFO), 
       new EchoServerHandler() 
     ); 
    } 

} 

答えて

1

ctx.pipeline().addLast(new EchoServerInitializer("localhost", 3020)); 

そしてEchoServerInitializer:私はまた、プロキシの例に類似これを、試してみました。プロキシの例では8443ポートを使用しているので、コマンドtelnet localhost 8443を使用してtelnet(またはsomethig)経由で接続できます。

+0

別のサーバーが稼動しています – Crystal

関連する問題