简体   繁体   中英

UDP packet: How to send different datatypes in one packet?

I am programming a multiplayer game and I am having a hard time with the size of udp packets. I want to send multiple enemy stats (hp, position, id) via udp packets. Here is the important code-part:

Byte[] datagramPacketData = ("11" + String.valueOf(entityAmount) + "," + gamename + "," + username + entitiesToBeSent).getBytes();

As you can see, i am merging all the data into a String and then convert it to byte[] before i send it. But chars consume much more bytes than little ints(for id and position) and longs(hp). For example: one mob has 2.000.000 hp. that number will consume 7 chars(=7 byte), but it would only need 4 byte for long. how can i send all this data by using up less bytes, without using object streams?

I would be very happy about some suggestions! Thank you very much in advance.

First of all, provided that your data is small enough to fit into a single packet, the UDP message length is unlikely to make much difference. I would recommend that you don't sweat on the issue unless you have concrete evidence that packet size is a critical issue; ie hard benchmarking results that show that you won't be able to sustain the minimum transmission rate required by the game above a certain packet size. (Optimizing for a problem that doesn't actually exist is a waste of effort.)

If you are concerned about message length, then you should compare using string encoding (as above) and binary encoding; eg like you would get using DataInputStream and DataOutputStream .

  • a byte will be encoded as a single octet
  • a short or char will be encoded as two octets
  • etc.

See the DataOutputStream javadocs for details.

Don't use object streams. They use the same binary encoding as DataOutputStream , but there are significant additional overheads for encoding type information and object identities.

I have made my attempt now and it works for String, but not or long. I cant figure out the mistake on my own, mayb eone of you can find the error? Here is it:

On sender side:

byte[] data = new byte[1024];
        int i = 0;

        String packetNumber = "25";

        ByteBuffer buffer0 = ByteBuffer.allocate(2);
        buffer0.put(packetNumber.getBytes());
        byte[] numberInByte = buffer0.array();

        int x = 0;
        for (int k = i; k < numberInByte.length; k++) {
            data[k] = numberInByte[x];
            x++;
            i++;
        }

        long life = 2000000;

        ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
        buffer.putLong(life);
        byte[] lifeInByte = buffer.array();

        x = 0;
        for (int k = i; k < lifeInByte.length; k++) {
            data[k] = lifeInByte[x];
            x++;
            i++;
        }

On receiver side:

ByteBuffer byteBuffer = ByteBuffer.wrap(data);

        byte[] packetNumber = new byte[2];
        byteBuffer.get(packetNumber);
        System.out.println(new String(packetNumber));

        long life = byteBuffer.getLong();
        System.out.println(life);

The output is: 25 (which is right) 1966080 (which is not quite 2000000, but why?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM