ChannelRead not invoked

201 Views Asked by At

I have an existing application which uses java sockets to connect to a C++ server and send request and read response , I am trying to change java sockets to net based connection ..

I have a netty client and I am sending the same request through Netty client , I can see the request is being sent and my channel read is not invoked for me to read the response from the server

I see FLUSH , READ COMPLETE , FLUSH , INACTIVE and UNREGISTERED in the netty logs .

I need help on why the clients channel read method is not invoked ..

I am using Netty 4.1.10.Final and below is the client and the client handler code

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


}

This is how i am sending the message where msg is byte array

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

There are 0 best solutions below