激光雷达作为服务端,一秒的间隔时间主动推送到netty开发的客户端。springboot项目启动时一切正常,拿到雷达数据并且解析,运行一段时间后,通达正常,但是客户端就拿不到数据了,服务端可能还在继续主动推送。
然后再过几个小时就报错了 Netty java.io.IOException: Connection reset by peer
客户端代码
@Override
public void run(String... args) throws Exception {
EventLoopGroup boss = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(boss)
.channel(NioSocketChannel.class)
//.option(ChannelOption.SO_KEEPALIVE,true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
// ch.pipeline().addLast("encoder",
// new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,
// 6,4,-10,0));
//处理接收HTTP报文不全的特殊设置
//ch.pipeline().addLast("aggregator", new HttpObjectAggregator(3200));
ch.pipeline().addLast("decoder", new SimpleChannelInboundHandler(){
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) {
ByteBuf buf = (ByteBuf) msg;
String s = ByteBufUtil.hexDump(buf);
String substring = s.substring(s.length() - 8, s.length());
if (!substring.equals("88aaaa88")){
log.info("原始数据:"+ s + "==============");
}
log.info("原始数据:"+ s);
// try{
// // 帧头 AA 88 88 AA
// long unsignedInt = buf.readUnsignedInt();
// // 主命令号
// short b = buf.readUnsignedByte();
// // 子命令号
// short b1 = buf.readUnsignedByte();
// // 数据帧长度
// long length = buf.readUnsignedInt();
// // 数据编号
// int i1 = buf.readUnsignedShort();
// // 起始角度
// int i2 = buf.readUnsignedShort();
// double aDouble = Double.valueOf(i2 / 100);
// // 角度步进
// int i3 = buf.readUnsignedShort();
// // 总距离个数
// int i4 = buf.readUnsignedShort();
// log.info("主命令号{};子命令号{};数据帧长度{};数据编号{};起始角度{};角度步进{};总距离个数{};",b,b1,length,i1,i2,i3,i4);
// for(int i=0;i<i4;i++){
// int i5 = buf.readUnsignedShort();
//// log.info("度数:{},距离:{}",i2+i,i5);
// Distance distance = new Distance(aDouble+i,i5,""+i1);
// influxMapper.save(distance);
// }
// }catch (Exception E){
// log.info("异常:{}",E);
// }
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.info("通道关闭: {}"+":::::::::::::::");
cause.printStackTrace();
ctx.close();
}
});
}
});
ChannelFuture future = bootstrap.connect("192.168.18.101", 4001).sync();
// 程序阻塞
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
log.info("异常2 :{}", e);
}finally {
boss.shutdownGracefully();
}
}