繁体   English   中英

如何将收到服务器的消息转发到Netty(Java)中的另一台服务器?

[英]How can I forward a message that received a server to another server in Netty (Java)?

我有一些客户端,它们与一台服务器通信,我需要该服务器将消息转发到另一台第二台服务器。 然后,从第二台服务器接收消息并发送给客户端。

使用这种方法,我实现了连接到第二台服务器,但是它没有收到消息,并引发了以下异常:

例外:java.nio.channels.NotYetConnectedException。 java.nio.channels.NotYetConnectedException

public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws IOException, Exception {
        response = "hola" + "\r\n";
        Main.creaLog("Mensaje recibido del conc: " + e.getMessage().toString());
        Main.creaLog("Mensaje enviado al servidor : " + response);


        ClientBootstrap bootstrap1 = new ClientBootstrap(
        new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool()));

        // Configure the pipeline factory.
        //bootstrap1.setPipelineFactory(new CLIENTE.ClientePipelineFactory());
        bootstrap1.setPipelineFactory(new ChannelPipelineFactory() {
         public ChannelPipeline getPipeline() {
                return Channels.pipeline(new ClienteHandler());
         }
        });

        final ChannelFuture future = bootstrap1.connect(new InetSocketAddress("172.16.10.14", 12355));

        Channel channel = future.getChannel();

        if (channel.isWritable()) {
            ChannelFuture lastWriteFuture = channel.write(e.getMessage().toString() + "\r\n");
        }
        close = true;


    // We do not need to write a ChannelBuffer here.
    // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
    ChannelFuture future = e.getChannel().write(response + "\r\n");
    //CIERRA LA CONEXION
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

如果有人能帮助我,我非常感谢。

看一下Netty Proxy的例子

现在,您基本上正在尝试在收到的每条消息上连接到远程服务器。 这可能不是您想要的。 您可能只想连接到远程服务器一次(即Netty代理示例中的出站通道),然后将新的传入消息转发到该特定通道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM