2013-02-12 163 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(); 
      } 
     } 
    }); 
} 

一旦连接到远程服务器,无论客户端发送(经由入站信道)被转发到远程服务器(出站信道)。

如果您还没有这样做,我建议您遵循并实施代理示例。