今天遇到使用netty做客户端的时候,发送数据,服务端接收不到,但是客户端不添加自己的hadler就可以,后来发现,由于的我的需要使用byte[]进行接收与发送,channelFuture.channel.writeAndFlush()需要使用ByteBuff发送,因此在outHandler接收的时候类型不对,outhandler接收的是inhandler发送过来的integer,造成了异常,因此需要在outhandler中先判断下类型,如果是int走我的模式,如果不是需要走其他模式
public class RTUClient {
?? ? ??? public void test(){ ??????? EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); ??????? Bootstrap bootstrap = new Bootstrap(); ??????? try { ??????????? bootstrap.group(eventLoopGroup) ??????????? .channel(NioSocketChannel.class) ??????????? .handler(new ChannelInitializer<Channel>() {
??????????????? protected void initChannel(Channel channel) throws Exception { ?????????????????? ? ??????????????????? channel.pipeline().addLast(new RTUOutHandler()); ??????????????????? channel.pipeline().addLast(new RTUInHandler()); ?????????????????? ? //??????????????????? channel.pipeline().addLast(new StringEncoder()); //??????????????????? channel.pipeline().addLast(new StringDecoder()); ??????????????? } ?????????????? ? ??????????? }); ?????????? ? ??????????? ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8086).sync(); ??????????? ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(); ??????????? byteBuf.writeBytes("ETUNG:12345678901234511".getBytes(StandardCharsets.UTF_8)); ??????????? channelFuture.channel().writeAndFlush(byteBuf); //??????????? channelFuture.channel().writeAndFlush("ETUNG:123456789012345"); //??????????? channelFuture.channel().writeAndFlush(Unpooled.buffer().writeByte(12)); ??????????? System.out.println("发送完毕"); ??????????? channelFuture.addListener(new ChannelFutureListener() { ?????????????? ? ??????????????? @Override ??????????????? public void operationComplete(ChannelFuture arg0) throws Exception { ??????????????????? System.out.println("发送成功"); ??????????????? } ??????????? }); //??????????? channelFuture.channel().closeFuture().sync(); ??????????? System.out.println("发送完毕222"); ??????? } catch (Exception e) { ??????????? eventLoopGroup.shutdownGracefully(); ??????? } ?????? ? ??? } ?? ? ??? public static void main(String[] args) { ??????? RTUClient rtuClient = new RTUClient(); ??????? rtuClient.test(); ??? } }
public class RTUInHandler extends ChannelInboundHandlerAdapter{ ?? ? ??? @Override ??? public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ?????? ? ??????? ByteBuf byteBuf = (ByteBuf)msg; ??????? int length = byteBuf.readableBytes(); ??????? byte[] receiveBytes = new byte[length]; ??????? byteBuf.readBytes(receiveBytes); ??????? System.out.println("ByteUtil.toString(receiveBytes):"+ByteUtil.toString(receiveBytes)); ??????? ReferenceCountUtil.release(msg); ?????? ? ??????? int step = 0; ??????? if(receiveBytes.length == 34){ ??????????? step = 0; ??????? }else{ ??????????? step = RTUUtil.bytesToInt(new byte[]{receiveBytes[3],receiveBytes[4],receiveBytes[5],receiveBytes[6]}); ??????? } ?????? ? ??????? ctx.writeAndFlush(step); ?????????????? ? //??????? ctx.writeAndFlush(msg); ??? }
}
public class RTUOutHandler extends ChannelOutboundHandlerAdapter{ ?? ? ??? @Override ??? public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { ??????? try{ ??????????? if(msg instanceof Integer){? //判断下类型,客户端首次发送是ByteBuffer类型 ??????????????? int step = (int) msg; ??????????????? byte[] bytes = RTUUtil.intToBytes(step); ??????????????? ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(); ??????????????? ByteBuffer byteBuffer = ByteBuffer.allocate(7); ??????????????? byteBuffer.put((byte)0XAA); ??????????????? byteBuffer.put((byte)0XBB); ??????????????? byteBuffer.put((byte)0X00); ??????????????? for(int i=0;i<bytes.length;i++){ ??????????????????? byteBuffer.put(bytes[i]); ??????????????? } ??????????????? byteBuffer.flip(); ??????????????? byte[] bytes1 = byteBuffer.array(); ??????????????? byte[] crc16 = CRC.swapCalcCrc16(bytes1); ??????????????? byteBuf.writeBytes(bytes1); ??????????????? byteBuf.writeBytes(crc16); ??????????????? super.write(ctx, byteBuf, promise); ??????????? }else{ ??????????????? super.write(ctx, msg, promise); ??????????? } ?????????? ? ??????? }catch(Exception ex){ ??????????? ex.printStackTrace(); ??????? } ??????? ?????? ? ??? }
}
|