EsotericSoftware / kryonet

TCP/UDP client/server library for Java, based on Kryo
BSD 3-Clause "New" or "Revised" License
1.81k stars 415 forks source link

KryoNetException: Error during deserialization. (But only sometimes) #149

Closed orange451 closed 5 years ago

orange451 commented 5 years ago

When I connect to my server using TCP from my client and start sending data, SOMETIMES I get this error:

Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization. at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141) at com.esotericsoftware.kryonet.Client.update(Client.java:247) at com.esotericsoftware.kryonet.Client.run(Client.java:333) at java.lang.Thread.run(Unknown Source) Caused by: com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: 18 at com.esotericsoftware.kryo.util.DefaultClassResolver.readClass(DefaultClassResolver.java:113) at com.esotericsoftware.kryo.Kryo.readClass(Kryo.java:613) at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:724) at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:58) at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139) ... 3 more

Sometimes when I run the application it works with no problem. Sometimes I am met with this error. I am not quite sure why. I know I am registering all the appropriate classes, as sometimes the application works without problem. Are there any suggestions anyone can give to help debug this?

crykn commented 5 years ago

I know I am registering all the appropriate classes, as sometimes the application works without problem.

That is not necessarily true. If you send an object whose members variables can hold different types (depending on the situation), this could explain your issue.

Kryonet itself doesn't have any log output in this area so your only options are:

  1. Add log statements yourself (in TcpConnection#readObject(Connection))/use the debugger (which is always a hassle dealing with networking)
  2. Disable the class registration requirement (this would fix your issue, but leaves the root cause untouched)
  3. Try to use a later version of Kryo (Kryonet still uses 2.24 and the latest version is 5.0.0) and see if that produces a better error/log message (just take a look at the various forks which have already done this)
orange451 commented 5 years ago

Thanks for the reply, @crykn All of the objects I am sending only contain Long and String fields.

I will look into logging the readobject method, thanks for the suggestion.

[Edit] I've updated to using a newer version of kryo. There's now debug output in the console. What appears to be happening is data is being sent before the client has registered their classes. Not quite sure how that is happening.

[Edit 2] I changed my code so that I register my classes before I attempt to establish a connection. I believe this has fixed it.