javalibgdxnetwork-programmingkryonet

Connecting client to server Kryonet and Libgdx


I'm trying to connect from a client which uses libgdx for the interface to a server. I use Kryonet on both the client and the server. However it is not working. Any idea what's wrong?

Here is the code that i use in the client:

     Client client = new Client();
        Kryo kryo = client.getKryo();
        kryo.register(String.class);
        client.start();
        new Thread() {
            public void run() {
                try {
                    client.connect(5000, "localhost", 54555, 54777);
                    client.addListener(new Listener() {
                        public void received(Connection connection, Object object) {
                            if (object instanceof String) {
                                String response = (String) object;
                                System.out.println(response);
                            }
                        }
                    });
                } catch (IOException e) {
                    Log.warn("expection", e);
                }
            }
        }.start();
   client.sendTCP("Hey There");

Here is the server code:

Server server = new Server();
    Kryo kryo = server.getKryo();
            kryo.register(String.class);
            server.start();
            try {
                server.bind(54555, 54777);
            } catch (IOException e) {
                e.printStackTrace();
            }

            server.addListener(new Listener() {
                public void received (Connection connection, Object object) {
                   if (object instanceof String) {
                      String request = (String)object;
                      System.out.println("Server"+request);

                      String response = "Login succes";
                      connection.sendTCP(response);
                   }
                }
             });

Here is the error that i get:

[kryonet] Error updating connection.
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(Unknown Source)
Exception in thread "Client" 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(Unknown Source)
00:05  WARN: expection
java.net.SocketTimeoutException: Connected, but timed out during TCP registration.
Note: Client#update must be called in a separate thread during connect.
    at com.esotericsoftware.kryonet.Client.connect(Client.java:168)
    at com.esotericsoftware.kryonet.Client.connect(Client.java:117)
    at me.fort.historywars.LoginScreen$1.run(LoginScreen.java:37)

Solution

  • Alright, I consider this code fixed:

    Server:

    server = new Server();
        Kryo kryo = server.getKryo();
        kryo.register(SRq.class);
        server.start();
        try {
            server.bind(54555, 54777);
        } catch (Exception e) {
            System.err.println("Failed to bind to port!");
        }
        server.addListener(new Listener() {
            @Override
            public void received(Connection connection, Object object) {
                if(object instanceof SRq) {
                    System.out.println("Server " +  ((SRq) object).data);
                    SRq sRq = new SRq();
                    sRq.data = "Data";
                    connection.sendTCP(sRq);
                }
            }
        });
    

    Client:

    client = new Client();
        Kryo kryo = client.getKryo();
        kryo.register(SRq.class);
        client.start();
        try {
            client.connect(6000, "localhost", 54555, 54777);
        } catch (Exception e) {
            System.err.println("Failed to connect to server!");
        }
        client.addListener(new Listener() {
            @Override
            public void received(Connection connection, Object object) {
                if(object instanceof SRq) {
                    Gdx.app.log("Client", ((SRq) object).data);
                }
            }
        });
        System.out.println("Connected to server!");
    
        SRq sRq = new SRq();
        sRq.data = "Log in";
        client.sendTCP(sRq);
    

    Things to keep in mind: