2013-02-12 157 views
1

我已閱讀netty代理服務器示例。不過,我想知道如何實現一個客戶端與代理交談。我正在實現的解決方案是服務器,只要客戶端連接到服務器,它就需要連接到套接字服務器。因此,連接到服務器的每個客戶端都能夠從另一個服務器發送/接收數據。Netty代理服務器

因爲服務器端是建立在netty上的,所以我需要幫助來用netty來實現這樣的體系結構。

回答

1

看來要實現可以通過Netty proxy example

下面的代碼段中幾乎回答顯示的內容,一旦新的客戶端通道打開,你可以如何連接到遠程服務器。

@Override 
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) 
     throws Exception { 
    // Suspend incoming traffic until connected to the remote host. 
    final Channel inboundChannel = e.getChannel(); 
    inboundChannel.setReadable(false); 

    // Start the connection attempt. 
    ClientBootstrap cb = new ClientBootstrap(cf); 
    cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel())); 
    ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort)); 

    outboundChannel = f.getChannel(); 
    f.addListener(new ChannelFutureListener() { 
     public void operationComplete(ChannelFuture future) throws Exception { 
      if (future.isSuccess()) { 
       // Connection attempt succeeded: 
       // Begin to accept incoming traffic. 
       inboundChannel.setReadable(true); 
      } else { 
       // Close the connection if the connection attempt has failed. 
       inboundChannel.close(); 
      } 
     } 
    }); 
} 

一旦連接到遠程服務器,無論客戶端發送(經由入站信道)被轉發到遠程服務器(出站信道)。

如果您還沒有這樣做,我建議您遵循並實施代理示例。