Packet ByteBuffer到通用数据包

时间:2015-03-22 04:04:28

标签: java sockets bytearray packet

我目前有以下代码来解码来自ByteBuffer

的ip数据包
    int buffer = packet.get();
    int headerlength;
    int temp;

    version = buffer >> 4;
    headerlength = buffer & 0x0F;
    headerlength *= 4;
    System.out.println("IP Version:" + version);
    System.out.println("Header Length:" + headerlength);
    String status = "";
    status += "Header Length:" + headerlength;

    buffer = packet.get();      //DSCP + EN
    buffer = packet.getChar();  //Total Length

    System.out.println("Total Length:" + buffer);

    buffer = packet.getChar();  //Identification
    buffer = packet.getChar();  //Flags + Fragment Offset
    buffer = packet.get();      //Time to Live
    buffer = packet.get();      //Protocol

    protocol = buffer;
    System.out.println("Protocol:" + buffer);

    status += "  Protocol:" + buffer;

    buffer = packet.getChar();  //Header checksum


    byte buff = (byte) buffer;

    sourceIP = "";
    buff = packet.get();  //Source IP 1st Octet
    temp = ((int) buff) & 0xFF;
    sourceIP += temp;
    sourceIP += ".";

    buff = packet.get();  //Source IP 2nd Octet
    temp = ((int) buff) & 0xFF;
    sourceIP += temp;
    sourceIP += ".";

    buff = packet.get();  //Source IP 3rd Octet
    temp = ((int) buff) & 0xFF;
    sourceIP += temp;
    sourceIP += ".";

    buff = packet.get();  //Source IP 4th Octet
    temp = ((int) buff) & 0xFF;
    sourceIP += temp;

    System.out.println("Source IP:" + sourceIP);

    status += "   Source IP:" + sourceIP;


    destIP = "";


    buff = packet.get();  //Destination IP 1st Octet
    temp = ((int) buff) & 0xFF;
    destIP += temp;
    destIP += ".";

    buff = packet.get();  //Destination IP 2nd Octet
    temp = ((int) buff) & 0xFF;
    destIP += temp;
    destIP += ".";

    buff = packet.get();  //Destination IP 3rd Octet
    temp = ((int) buff) & 0xFF;
    destIP += temp;
    destIP += ".";

    buff = packet.get();  //Destination IP 4th Octet
    temp = ((int) buff) & 0xFF;
    destIP += temp;

它解析了主机名,目标IP,源IP,版本,协议和端口,但我希望获得数据包的所有内容。

我自己没有写这段代码所以我不完全理解它。我明白了ByteArray packet中的不同位包含不同的数据,具体取决于位的位置。唯一的问题是我不知道如何使用该逻辑检索数据。

0 个答案:

没有答案