Search code examples
androidandroid-studiokryonet

Android game with server/client not working using kryonet


I have been trying for days to work out whats wrong, changing things etc, has been of no avail.

Get the following error in android studio, when trying to debug client side with my Samsung device:

com.esotericsoftware.kryonet.KryoNetException: Incorrect number of bytes (1 remaining) used to deserialize object: null
        at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:146)
        at com.esotericsoftware.kryonet.Client.update(Client.java:255)
        at com.esotericsoftware.kryonet.Client.run(Client.java:338)
        at java.lang.Thread.run(Thread.java:856)

I think it uses the same java version, and I register the class exactly the same. Client side:

     Client client = new Client();
            Kryo kryo = client.getKryo();
            kryo.register(SomeRequest.class);
            kryo.register(SomeResponse.class);
            client.start();
            try{
                client.connect(5000, "10.0.0.4", 31055, 32055);
            }catch (IOException e) {
                throw new GdxRuntimeException(e);
            }

            SomeRequest request = new SomeRequest();
            request.text = "Here is the request";
            client.sendTCP(request);

            client.addListener(new Listener() {
                public void received (Connection connection, Object object) {
                    if (object instanceof SomeResponse) {
                        SomeResponse response = (SomeResponse)object;
                        System.out.println(response.text);
                    }
                }
            });

Server side:

Server server = new Server();
        Kryo kryo = server.getKryo();
        kryo.register(SomeRequest.class);
        kryo.register(SomeResponse.class);
        server.start();
        System.out.println("server started");
        server.bind(31055, 32055);
        server.addListener(new Listener(){
            public void connected(Connection connection){
                System.out.println("connect");
            }

               public void received (Connection connection, Object object) {
                   if (object instanceof SomeRequest) {
                      SomeRequest request = (SomeRequest)object;
                      System.out.println(request.text);

                      SomeResponse response = new SomeResponse();
                      response.text = "Thanks";
                      connection.sendTCP(response);
                   }
                }
        });

    }

classes:

class SomeRequest{
    public String text;
}
class SomeResponse {
    public String text;
}

As you can see its very basic code, but to connect is a nightmare. Please help!! I am creating the client side in android studio, and the server im creating in eclipse. Could this be a reason for problems? Its the only logical reason I've been able to conceive. How can I solve that?


Solution

  • I had this as well and it could have been caused by three things.

    1. The server uses a different build/release of KryoNet.
    2. You are compiling with a different Java version.
    3. You are using two different IDE's to build the server and client.

    I actually had build my server with Eclipse since I was not aware I could create Java apps with Android Studio where I was building my client app with. This simultaneously caused a version difference since I was using Gradle for the client to include KryoNet and manually imported the closest version I could find in Eclipse.

    I got some help on the LibGDX IRC channel where the devs often hang out. They didn't think the version difference would be a problem (but it could). They said whenever I build a client and server they should use the same build system. This obvious tackles a lot of unforeseen errors.

    The moment I got a basic Java Project running in Android studio I copied the code included the exact same KryoNet dependency as the client in Gradle everything worked as expected. On top of that I love Android Studio and am very happy to use it for any Java Projects now.