2017-05-19 7 views
0

Netty에 구축중인 서버에서 다른 클라이언트에 연결하려고합니다.채널 핸들러에서 netty 클라이언트에 연결

http://netty.io/4.1/xref/io/netty/example/proxy/package-summary.html 그래서 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이 호출되지 않습니다. 다음

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

그리고 EchoServerInitializer : 나는 또한 프록시 예를 더 유사이 시도

당신은 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

. 프록시 예제는 8443 포트를 사용하므로 명령 telnet localhost 8443을 사용하여 telnet (또는 somethig)을 통해 연결할 수 있습니다.

+0

다른 서버가 가동 중입니다. – Crystal