java socketserver服务端接收C客户端发送的结构体数据

JAVA接收结构体数据


java socketserver服务端接收C客户端发送的结构体数据_第1张图片

java服务端解析对应结构体数据

 class RecvStruct{
     
        byte[] recv;
        private int velocity;
        private int acceleration;
        private int altitude;
        private long lon;
        private long lat;
        private int direction;
        private int current;
        private int command1;
        private int command2;
        private int command3;
        private int command4;
        private int command5;

        private int Msg_ID;
        private int Dst_ID;
        private int Src_ID;
        private int Msg_Len;

        RecvStruct(byte[] recv) {
     
            this.recv = recv;
        }

        /*
        解析收到的结构体信息
         */
        void parseFrame(){
     
            InputStream in_withcode;
            try {
     
//                for (int i=0;i<64;i++){
     
//                    Log.i("connection","recv["+i+"]"+recv[i]);
//                }
                in_withcode = new ByteArrayInputStream(recv);
//                Log.i("connection",in_withcode.toString());
                DataInputStream Datainput = new DataInputStream(in_withcode);

                int[] recvdata = new int[16];
                Msg_ID = Datainput.readUnsignedByte();
                Dst_ID = Datainput.readUnsignedByte();
                Src_ID = Datainput.readUnsignedByte();
                Msg_Len = Datainput.readUnsignedByte();
                Log.i("connection", String.valueOf("信息ID:"+Msg_ID+"  目的ID:"+Dst_ID+"  源ID:"
                        +Src_ID+"  信息大小:"+Msg_Len));
                Datainput.skipBytes(4);//跳过4个字节

                lon = longToBytes_Little(Datainput.readLong());
                lat = longToBytes_Little(Datainput.readLong());

                Log.i("connection", String.valueOf("Lon:"+lon+"  lat:"+lat));
                velocity = intToBytes_Little(Datainput.readInt());
                acceleration = intToBytes_Little(Datainput.readInt());
                altitude = intToBytes_Little(Datainput.readInt());
                direction = intToBytes_Little(Datainput.readInt());
                current = intToBytes_Little(Datainput.readInt());

                Log.i("connection", String.valueOf("速度:"+velocity+"  加速度:"+acceleration
                        +"  海拔:"+altitude+"  方向:"+direction+"  电流:"+current));
                command1 = Datainput.readUnsignedByte();
                command2 = Datainput.readUnsignedByte();
                command3 = Datainput.readUnsignedByte();
                command4 = Datainput.readUnsignedByte();
                command5 = Datainput.readUnsignedByte();
                Log.i("connection", String.valueOf("命令1:"+command1+"  命令2:"+command2+"  命令3:"
                        +command3+"  命令4:"+command4+"  命令5:"+command5));
                recvdata[0]=velocity;
                recvdata[1]=acceleration;
                recvdata[2]=command1;
                recvdata[3]=command2;
                recvdata[4]=command3;
                recvdata[5]=command4;
                recvdata[6]=command5;
                myCallBack.InfoNewMessage(recvdata);

            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }

        /**
         * long转byte数组,小端模式
         * @param value
         * @return
         */
        long longToBytes_Little(long value) {
     
            byte bytes[] = new byte[8];
            bytes[7] = (byte)  (0xff & (value >> 56));
            bytes[6] = (byte)  (0xff & (value >> 48));
            bytes[5] = (byte)  (0xff & (value >> 40));
            bytes[4] = (byte)  (0xff & (value >> 32));
            bytes[3] = (byte)  (0xff & (value >> 24));
            bytes[2] = (byte)  (0xff & (value >> 16));
            bytes[1] = (byte)  (0xff & (value >> 8));
            bytes[0] = (byte)  (0xff & value);

            return (0xff00000000000000L & ((long) bytes[0] << 56)
                    | (0xff000000000000L & ((long) bytes[1] << 48))
                    | (0xff0000000000L & ((long) bytes[2] << 40))
                    | (0xff00000000L & ((long) bytes[3] << 32))
                    | (0xff000000L & ((long) bytes[4] << 24))
                    | (0xff0000L & ((long) bytes[5] << 16))
                    | (0xff00L & ((long) bytes[6] << 8)) | (0xffL & (long) bytes[7]));
        }

        /**
         *
         * @param value
         * @return
         */
        int intToBytes_Little(int value) {
     
            byte[] src = new byte[4];
            src[0] = (byte) ((value>>24)&0xFF);
            src[1] = (byte) ((value>>16)&0xFF);
            src[2] = (byte) ((value>>8)&0xFF);
            src[3] = (byte) ((value)&0xFF);
            return ((src[0]&0xFF)|((src[1]&0xFF)<<8)|((src[2]&0xFF)<<16)|((src[3]&0xFF)<<24));
        }

你可能感兴趣的:(Java学习)