UDP解析

来源:互联网 发布:淘宝投诉商家电话号码 编辑:程序博客网 时间:2024/06/01 09:05
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ParseDate {

    public static void main(String[] args) throws IOException {
        // 调用静态方法Recv();
        Recv();
    }

    public static void Recv() throws IOException {
        System.out.println("接收开始......");
        DatagramSocket ds = new DatagramSocket(40001);// 创建DatagramSocket实例,其中DatagramSocket类用于创建接收和发送UDP的Socket实例,40001是端口号
        while (true) {
            // 接收数据的buf数组并指定大小
            byte[] buf = new byte[46];
            // 创建接收数据包,存储在buf中
            DatagramPacket dp = new DatagramPacket(buf, buf.length);
            // 接收操作
            ds.receive(dp);
            // 帧头,占4字节,字节数组转换为字符串类型
            String locl = (String.valueOf((char) (buf[0]))
                    + String.valueOf((char) (buf[1]))
                    + String.valueOf((char) (buf[2])) + String
                    .valueOf((char) (buf[3])));
            // 长度(不包括帧头长度),占两个字节,字节数组转换为整数类型数据
            int headLength = ((int) (buf[4]) + (int) (buf[5] << 8));
            
            // 类型,占两个字节,字节数组转换为整数类型数据
            int dateType = ((int) (buf[6]) + (int) (buf[7] << 8));
            
            // 楼层,占两个字节,字节数组转换为整数类型数据
            int floor = ((int) (buf[8]) + (int) (buf[9] << 8));
            
            // 标签ID,占两个字节,字节数组转换为整数类型数据
            int tagId = ((int) (buf[10]) + (int) (buf[11] << 8)
                    + (int) (buf[12] << 16) + (int) (buf[13] << 24));
            
            // X坐标,占八个字节,调用方法bytesToLong(byte[] b),把字节数组转换为小数点类型
            byte[] tempByte = new byte[8];
            for (int i = 0, n = 14; i < tempByte.length; i++, n++) {
                tempByte[i] = buf[n];
            }
            double Xpos = Double.longBitsToDouble(bytesToLong(tempByte));
            
            // Y坐标,占八个字节,调用方法bytesToLong(byte[] b),把字节数组转换为小数点类型
            for (int i = 0, n = 22; i < tempByte.length; i++, n++) {
                tempByte[i] = buf[n];
            }
            double Ypos = Double.longBitsToDouble(bytesToLong(tempByte));
            
            // Z坐标,占八个字节,调用方法bytesToLong(byte[] b),把字节数组转换为小数点类型
            for (int i = 0, n = 30; i < tempByte.length; i++, n++) {
                tempByte[i] = buf[n];
            }
            double Zpos = Double.longBitsToDouble(bytesToLong(tempByte));
            
            // 保留字节,占八个字节,把字节数组转换为字符串类型
            String KeepByte = (String.valueOf((char) (buf[38]))
                    + String.valueOf((char) (buf[39]))
                    + String.valueOf((char) (buf[40]))
                    + String.valueOf((char) (buf[41]))
                    + String.valueOf((char) (buf[42]))
                    + String.valueOf((char) (buf[43]))
                    + String.valueOf((char) (buf[44])) + String
                    .valueOf((char) (buf[45])));
            String ip = dp.getAddress().getHostAddress();//得到发送数据的ip地址
            int port = dp.getPort();//端口号
        }
    }

    /**
     * 将长度为8的byte数组转换为一个long类型值.
     *
     * @param b
     * @return
     */
    public static long bytesToLong(byte[] b) {
        long l = ((long) b[7] << 56) & 0xFF00000000000000L;
        // 如果不强制转换为long,那么默认会当作int,导致最高32位丢失
        l |= ((long) b[6] << 48) & 0xFF000000000000L;
        l |= ((long) b[5] << 40) & 0xFF0000000000L;
        l |= ((long) b[4] << 32) & 0xFF00000000L;
        l |= ((long) b[3] << 24) & 0xFF000000L;
        l |= ((long) b[2] << 16) & 0xFF0000L;
        l |= ((long) b[1] << 8) & 0xFF00L;
        l |= (long) b[0] & 0xFFL;
        return l;
    }

}

0 0
原创粉丝点击