How to get data from network packet data in Java

In C, if you have a specific type of package, what you usually do is define some structure and direct char * to a pointer to the structure. After that, you have direct programmatic access to all data fields in the network packet. For example:

struct rdp_header {
  int version;
  char serverId[20];
};

When you receive a network packet, you can do the following quickly:

char * packet;
// receive packet
rdp_header * pckt = (rdp_header * packet);
printf("Servername : %20.20s\n", pckt.serverId);

This method works great for UDP-based protocols and allows very fast and very efficient parsing and sending packets using very little code and trivial error handling (just check the packet length). Is there an equivalent, just like a quick way in java to do the same? Or are you forced to use stream methods?

+5
6

, .

, :

DatagramSocket s = new DatagramSocket(port);
DatagramPacket p;
byte buffer[] = new byte[4096];

while (true) {
    p = new DatagramPacket(buffer, buffer.length);
    s.receive(p);

    // your packet is now in buffer[];
    int version = buffer[0] << 24 + buffer[1] << 16 + buffer[2] < 8 + buffer[3];
    byte[] serverId = new byte[20];
    System.arraycopy(buffer, 4, serverId, 0, 20);

     // and process the rest
}

, , Tom , ByteArrayInputStream(), DataInputStream(), :

...

while (true) {
    p = new DatagramPacket(buffer, buffer.length);
    s.receive(p);

    ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
    DataInput di = new DataInputStream(bais);

    int version = di.readInt();
    byte[] serverId = new byte[20];
    di.readFully(serverId);
    ...
}
+3

, Java, JNI C. - , Java .

( ), (. xml), ( ) , .

( ).

, , , .

+2

-, . , , .

. , :

    // Resulting byte array is 9 bytes long.
    byte[] ba = new ByteArrayBuilder()

     .writeInt(0xaaaa5555) // 4 bytes
     .writeByte(0x55) //      1 byte
     .writeShort(0x5A5A) //   2 bytes
     .write( (new BitBuilder())  //     2 bytes---0xBA12                
            .write(3, 5) //     101      (3 bits value of 5)
            .write(2, 3) //        11    (2 bits value of 3)
            .write(3, 2) //          010 (...)
            .write(2, 0) //     00
            .write(2, 1) //       01
            .write(4, 2) //         0002
        ).getBytes();

ByteArrayBuilder, . ( "this" ), .

ByteArrayBuilder , 1 2 ( )

, , .

BitBuilder:

public BitBuilder write(int bitCount, int value) {
    int bitMask=0xffffffff;  
    bitMask <<= bitCount;   // If bitcount is 4, bitmask is now ffffff00
    bitMask = ~bitMask;     // and now it 000000ff, a great mask

    bitRegister <<= bitCount; // make room
    bitRegister |= (value & bitMask); // or in the value (masked for safety)
    bitsWritten += bitCount;
    return this;
}

, .

edit: , , .

+1

Javolution , , . , , Javolution Struct UDP.

+1

, . , , C, .

:

OneByte,       1
OneBit,       .1
TenBits,      .10
AlsoTenBits,  1.2
SignedInt,    +4  

, , . , -

:

new PacketReader packetReader("PacketStructure.txt", byte[] packet);

PacketStructure.txt - ( , ) .

, bitStructure , , :

int x=packetReader.getInt("AlsoTenBits");

, , C, , - , , , . , , - , , - .

, , , .

0

, , .

, Serializable, InputStream ObjectInputStream . . , TCP Socket. UDP DatagramSocket, ByteArrayInputStream.

, , , , readObject() writeObject(), , . (, ), , , , .

, , Java UTF-16 , , . , -Java-.

0

All Articles