Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接到channelhandler中netty中的客户端_Java_Netty - Fatal编程技术网

Java 连接到channelhandler中netty中的客户端

Java 连接到channelhandler中netty中的客户端,java,netty,Java,Netty,我正在尝试从我在Netty中构建的服务器连接到另一个客户端。我在这里查看了代理示例: 所以在我的ChannelInboundHandlerAdapter子类中,我尝试这样做 ctx.pipeline().addLast(new EchoTestHandler("localhost", 3030)); 我的EchoTestHandler看起来像: public class EchoTestHandler extends ChannelInboundHandlerAdapter { pr

我正在尝试从我在Netty中构建的服务器连接到另一个客户端。我在这里查看了代理示例:

所以在我的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:

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()
        );
    }

}

您需要连接到代理服务器以执行channelActive调用。代理示例使用8443端口,所以您可以使用命令telnet localhost 8443通过telnet或其他方式进行连接。

我有另一台服务器正在运行