Home » Android » Kryonet Client immediately disconnecting from server

Kryonet Client immediately disconnecting from server

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am currently in the process of writing an app which has the phone connect to a server.

Both client and server are using the Kryonet framework.

The problem ist the following :

When the server is running and I then start up the client, the client immediately disconnects from the server but the Programm itself keeps running so it is only possible that the client Thread died for whatever reason.

I am using kryonet-2.21 on both server and client.
I tried my code on Android aswell as on pc.
I also tried to troubleshoot everything I could and tried everything I found searching for my problem.

The Client code :

public class LogicHandler extends Thread {
    private Client client;

    public LogicHandler() {
    }

    public Client getClient() {
        return client;
    }

    public void run() {
        client = new Client(33554432, 33554432);
        new Thread(client).start();

        try {
            getClient().connect(5000, "localhost", 54555, 54777);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Packets.register(getClient());

        getClient().addListener(new Listener() {
            public void received(Connection connection, Object object) {
                System.out.println("received " + object);
                if (object instanceof ConnectionResponse) {

                }
                if (object instanceof ScheduleResponse) {

                }
            }

            public void disconnected(Connection connection) {

            }

        });

        getClient().sendTCP(new ConnectionRequest());
    }

    public static void main(String[] args) {
        new LogicHandler().start();
        while(true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("wait");
        }
    }
}

The Server code :

public class ServerLogicHandler {

    private Server server;

    private List<Client> clients;
    private List<Connection> connections;

    public ServerLogicHandler() {
        System.out.println("Server is starting!");
        server = new Server(33554432, 33554432);
        server.start();

        try {
            server.bind(54555, 54777);
        } catch (Exception e) {
            server.stop();
            System.out.println("Port belegt. Server wird gestoppt!");
            System.exit(0);
        }

        Packets.register(server);
        clients = new ArrayList<Client>();
        connections = new ArrayList<Connection>();

        server.addListener(new Listener() {
            public void received(Connection connection, Object object) {
                System.out.println("got packet");
                if (object instanceof ConnectionRequest) {
                    System.out.println("size  " + connection.sendTCP(new ScheduleResponse()));
                }
            }

            public void disconnected(Connection connection) {
                System.out.println("Disco " + connection.getID());
            }

            public void connected(Connection connection) {
                System.out.println(connection.getRemoteAddressTCP().getPort() + " "
                        + connection.getRemoteAddressTCP().getAddress());
            }

        });

        System.out.println("Server started!");
    }

    public Server getServer() {
        return server;
    }

    public static void main(String... args) {
        new ServerLogicHandler();
    }

}

The client doesn’t output anything apart from the ‘wait’ every second. This means that either the server didn’t send any packet or the client closed already. My guess is that the latter happened because the server outputs the following :

Server is starting!
Server started!
54408 /127.0.0.1
Disco 1

When I start a new client the last 2 lines would just repeat e.g. ‘54890 /127.0.0.1
Disco 2

From this I guess that the client closes for whatever reason before even sending any packets. None of my Google searches brang up any success.

Answers: