简体   繁体   English

多次发送自定义对象后,Kryonet断开连接

[英]Kryonet disconnects after sending custom object more than once

I'm learning some networking stuff with KryoNet and I have this issue that when I send object from server to client it sends it just fine and I can read it, but when i send it again I get this error. 我正在学习KryoNet的一些网络知识,并且遇到了以下问题:当我从服务器向客户端发送对象时,它可以很好地发送并且可以读取它,但是当我再次发送它时,出现此错误。

Server: 服务器:

server = new Server();
    Kryo kryo = server.getKryo();
    kryo.register(Command.class, new JavaSerializer());
    server.start();
    try {
        server.bind(54555, 54777);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Client: 客户:

client = new Client();
    Kryo kryo = client.getKryo();
    kryo.register(Command.class, new JavaSerializer());
    client.setKeepAliveTCP(2000);
    client.start();
    try {
        client.connect(5000, "192.168.1.5", 54555, 54777);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    client.addListener(new Listener() {
        public void connected(Connection connection){

        }
        public void received (Connection connection, Object object) {
            if(object instanceof Command){
                Command c = (Command) object;
                textField.setText(Integer.toString(c.getTime()));
            }
        }
     });

MyClass: 我的课:

public class Command implements Serializable{

private static final long serialVersionUID = 1L;
private int time;

public Command(int time) {
    setTime(time);
}

public int getTime() {
    return time;
}

public void setTime(int time) {
    this.time = time;
}

} }

StackTrace: 堆栈跟踪:

Exception in thread "Client" com.esotericsoftware.kryo.KryoException: Error during Java deserialization.
at com.esotericsoftware.kryo.serializers.JavaSerializer.create(JavaSerializer.java:42)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:758)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:57)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:137)
at com.esotericsoftware.kryonet.Client.update(Client.java:239)
at com.esotericsoftware.kryonet.Client.run(Client.java:317)
at java.lang.Thread.run(Unknown Source)




Caused by: java.io.StreamCorruptedException: invalid stream header: 79737200
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at com.esotericsoftware.kryo.serializers.JavaSerializer.create(JavaSerializer.java:40)
... 6 more

Have you tried checking what values you are getting from the server in the received method? 您是否尝试过以收到的方法检查从服务器获取的值?

I think it may be the case that since you are implementing Serializable interface and hence the data is getting serialized and when you are trying to convert it to String from integer, it is causing problems. 我认为可能是这种情况,因为您正在实现Serializable接口,因此数据正在被序列化,并且当您尝试将其从整数转换为String时,会引起问题。

Try to deserialize the data you receive and see what happens. 尝试反序列化收到的数据,看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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