0

我有一个现有的应用程序,它使用 Java 套接字连接到 C++ 服务器并发送请求和读取响应,我正在尝试将 Java 套接字更改为基于网络的连接..

我有一个 netty 客户端,我正在通过 Netty 客户端发送相同的请求,我可以看到请求正在发送,并且没有调用我的频道读取来读取来自服务器的响应

我在 netty 日志中看到 FLUSH、READ COMPLETE、FLUSH、INACTIVE 和 UNREGISTERED。

我需要关于为什么不调用客户端通道读取方法的帮助..

我正在使用 Netty 4.1.10.Final 及以下是客户端和客户端处理程序代码

public ChannelFuture connectLoop() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap clientBootstrap = new Bootstrap();

        clientBootstrap.group(group);
        clientBootstrap.channel(NioSocketChannel.class);
        clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        clientBootstrap.option(ChannelOption.TCP_NODELAY, true);
        clientBootstrap.remoteAddress(new InetSocketAddress("127.0.0.1", 8888));
        clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                //socketChannel.pipeline().addLast( new StringDecoder() ,new StringEncoder()  , new LineBasedFrameDecoder(1024), new ChannelHandlerAdapter(){
                socketChannel.pipeline().addLast("framer", new LengthFieldBasedFrameDecoder(1000000,0,4,0,4));//16KB

                socketChannel.pipeline().addLast("logger", loggingHandler);
               socketChannel.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
               socketChannel.pipeline().addLast("bytesDecoder", new ByteArrayDecoder());

                // Encoder
                socketChannel.pipeline().addLast("frameEncoder", new LengthFieldPrepender(4));
                socketChannel.pipeline().addLast("bytesEncoder", new ByteArrayEncoder());
                socketChannel.pipeline().addLast(new NettyClientHandler());




            }
        });
        ChannelFuture channelFuture = clientBootstrap.connect().sync();

        this.channel = channelFuture.channel();
        //channelFuture.channel().closeFuture().sync();
        return channelFuture;

    } finally {

    }



}


    public void shutdown() {
        workGroup.shutdownGracefully();
    }

}



public class NettyClientHandler extends ChannelInboundHandlerAdapter {

   @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Invoked -------------- > channel read complete");
        ctx.flush();
    }

    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("Invoked -------------- > Channel Active");


        super.channelActive(channelHandlerContext);

    }



    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable cause){
        cause.printStackTrace();
        channelHandlerContext.close();
    }


    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
        System.out.println("Invoked -------------- > channel read");
        System.out.println("Client received:" + msg.toString());
        channelHandlerContext.close();
    }


}

这就是我发送消息的方式,其中 msg 是字节数组

NettyClient nettyClient = new NettyClient(10500);
        ChannelFuture channelFuture = nettyClient.connectLoop();
        channelFuture.channel().writeAndFlush((msg)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
4

0 回答 0