0

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

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

例外: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);
    }
}

如果有人可以帮助我,我将非常感谢。

4

1 回答 1

1

看看Netty 代理示例

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

于 2013-02-19T04:49:04.563 回答