Netty 未调用ChannelRead 我有一个现有的应用程序,它使用java套接字连接到C++服务器,发送请求和读取响应,我尝试将java套接字转换成基于网络的连接。
我有一个netty客户端,我正在通过netty客户端发送相同的请求,我可以看到请求正在发送,并且没有调用我的通道读取来读取服务器的响应 我在netty日志中看到FLUSH、READ COMPLETE、FLUSH、INACTIVE和unregisted 我需要关于为什么不调用clients channel read方法的帮助 我使用的是Netty 4.1.10.Final,下面是客户端和客户端处理程序代码Netty 未调用ChannelRead 我有一个现有的应用程序,它使用java套接字连接到C++服务器,发送请求和读取响应,我尝试将java套接字转换成基于网络的连接。,netty,netty-socketio,Netty,Netty Socketio,我有一个netty客户端,我正在通过netty客户端发送相同的请求,我可以看到请求正在发送,并且没有调用我的通道读取来读取服务器的响应 我在netty日志中看到FLUSH、READ COMPLETE、FLUSH、INACTIVE和unregisted 我需要关于为什么不调用clients channel read方法的帮助 我使用的是Netty 4.1.10.Final,下面是客户端和客户端处理程序代码 public ChannelFuture connectLoop() throws Exce
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();
}
}
完成后,
writeAndFlush
返回的ChannelFuture
的结果是什么?ChannelFuture的结果是成功的我将在ChannelPipeline
中添加一个LoggingHandler
作为第一个,然后查看是否收到任何消息。管道atm中有很多处理程序,因此很难判断哪个在缓冲。感谢诺曼的回复,添加logginghandler作为通道管道中的第一行,可以得到相同的日志。。我看到一个刷新,然后一个读取完成。。我看不到频道被调用了。。还有其他建议吗?还有其他想法吗?我被这件事缠住了,不能继续下去
NettyClient nettyClient = new NettyClient(10500);
ChannelFuture channelFuture = nettyClient.connectLoop();
channelFuture.channel().writeAndFlush((msg)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);