Search code examples
javaarrayspacketkryokryonet

Sending and receiving arrays in kryonet


I'm using kryonet to create a Server/Client system.

I'm trying to send an int[][] to my client, from the server. To achieve this task I tried this:

publc class ArrayPacket {
 public int[][] array
}

Then, in the server listener I wrote:

ArrayPacket myPacket = new ArrayPacket();
for (int y = 0; y < Level.height; y++) {
    for (int x = 0; x < Level.width; x++) {
        myPacket.id[x][y] = somethingAt[x][y];
    }
}
clientConnection.sendTCP(myPacket);

Finally, in the client listener I receive the ArrayPacket:

public void received(Connection con, Object object) {
            if (object instanceof ArrayPacket) {
                for (int y = 0; y < Level.height; y++) {
                    for (int x = 0; x < Level.width; x++) {             
                        somethingAt[x][y] = ((ArrayPacket) object).array[x][y];
                    }
                }
            }
        }

And I register everything like that:

public void register(Kryo k) {
    k.register(ArrayPacket.class);
    k.register(int[][].class);
}

However, when I run it throws me an error saying that I must register the int[].class. So I do that, run the program again, and the compiler throws me a BufferOverflowException:

Exception in thread "Server" com.esotericsoftware.kryo.KryoException: java.nio.BufferOverflowException
Serialization trace:
array (myPackage.ArrayPacket)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:585)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
    at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:571)
    at com.esotericsoftware.kryonet.KryoSerialization.write(KryoSerialization.java:50)
    at com.esotericsoftware.kryonet.TcpConnection.send(TcpConnection.java:192)
    at com.esotericsoftware.kryonet.Connection.sendTCP(Connection.java:59)
    at myPackage.MyServerListener.received(ServerListener.java:28)
    at com.esotericsoftware.kryonet.Server$1.received(Server.java:61)
    at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246)
    at com.esotericsoftware.kryonet.Server.update(Server.java:208)
    at com.esotericsoftware.kryonet.Server.run(Server.java:356)
    at java.lang.Thread.run(Thread.java:724)

Caused by: java.nio.BufferOverflowException
    at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:183)
    at com.esotericsoftware.kryo.io.ByteBufferOutputStream.write(ByteBufferOutputStream.java:42)
    at com.esotericsoftware.kryo.io.Output.flush(Output.java:154)
    at com.esotericsoftware.kryo.io.Output.require(Output.java:134)
    at com.esotericsoftware.kryo.io.Output.writeInt(Output.java:246)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$IntArraySerializer.write(DefaultArraySerializers.java:55)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$IntArraySerializer.write(DefaultArraySerializers.java:45)
    at com.esotericsoftware.kryo.Kryo.writeObjectOrNull(Kryo.java:552)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$ObjectArraySerializer.write(DefaultArraySerializers.java:312)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$ObjectArraySerializer.write(DefaultArraySerializers.java:293)
    at com.esotericsoftware.kryo.Kryo.writeObjectOrNull(Kryo.java:552)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:570)
    ... 11 more

So, how can I fix this error?

Thanks in advance.


Solution

  • Well, I solved the problem. It was pretty simple: I just increased the amount of data that the server and the client can receive.