java - 在kryonet中发送和接收数组

原文 标签 java arrays packet kryo kryonet

我正在使用kryonet创建一个服务器/客户端系统。
我正在尝试从服务器向我的客户端发送int[][]。为了完成这项任务,我尝试了:

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

然后,在服务器侦听器中我写道:
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);

最后,在客户端侦听器中,我接收到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];
                    }
                }
            }
        }

我把所有东西都登记了:
public void register(Kryo k) {
    k.register(ArrayPacket.class);
    k.register(int[][].class);
}

但是,当我运行它时,会抛出一个错误,告诉我必须注册int[].class。所以我这样做,再次运行程序,编译器抛出一个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

那么,我怎样才能纠正这个错误呢?
提前谢谢。

最佳答案

我解决了这个问题。这很简单:我只是增加了服务器和客户机可以接收的数据量。

相关文章:

java - Java Android开发:引用其他类的静态字符串获取Intent参数是一种不好的做法吗?

c - 运算符[]的正确输入数据类型是什么?

c - 将char分配给char *

php - 一个数组中有两个单独的API调用值

java - 如何读取Java流,直到达到特定字节

java - Android:将文本输入转换为ASCII并将其打印为字符串

java - 提交类的实例作为参数

java - 当我为此按钮设置“可见性”时,出现了一个混蛋

java - 常规套接字问题 - 将C ++结构从Java转移到C ++

java - 增加与MySQL DB的Java连接的客户端数据包大小