我在使用netty5做通讯框架,长联接。连续发送多个请求,服务端每个请求后也执行 ctx.writeAndFlush(buildResponse(message, body));服务端居然将几个请求处理完了后。端户端才会有反应。哪位能帮解决一下,实现writeAndFlush后客户端能立即收到响应。
Server
ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChildChannelHandler()); f = b.bind(port).sync(); System.out.println("Netty Server start ok:" + (NettyConstant.REMOTEIP + ":" + port)); f.channel().closeFuture().sync();
client
Bootstrap b = new Bootstrap(); // b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() { b.group(group).channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel c) throws Exception { // -8表示lengthAdjustment,让解码器从0开始截取字节,并且包含消息头 c.pipeline().addLast(new NettyMessageDecoder(Integer.MAX_VALUE, 4, 4, -8, 0)); c.pipeline().addLast(new NettyMessageEncoder()); c.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); // c.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); c.pipeline().addLast("LoginAuthHandler", new LoginAuthReqHandler()); c.pipeline().addLast(new InitHandler()); c.pipeline().addLast(new ClientHandler()); } }); //此外可以发送多个目标地址 ChannelFuture f = b.connect(host, port).sync(); System.out.println("Netty time Client connected at port " + port); f.channel().closeFuture().sync();
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
好先进啊,我们都4######应该是发生粘包了吧######参考netty权威指南上写的。非常感谢两位.不过已经解决了。在后台加了一个newSingleThreadExecutor就可以立刻返回数据。而不会拼在一块了。 ######
你是说server端的childHandler吧?使用线程池去执行业务hander?