简体   繁体   中英

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.

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.

Try to deserialize the data you receive and see what happens.

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