Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kryonet disconnects just after connecting

Tags:

java

kryo

kryonet

I followed this Youtube tutorial covering the basics of Kryonet.

Basically it is a Kryonet Hello World, it explains how to setup a basic Server and a Client, allowing the Client to send Packets to the Server and have very basic communication.

A link to the source code. Both Server and Client have the same Packet class.

I can get the Server running, and the Client asking the IP to connect. However when i enter the IP, the Client terminates just after connecting.

Client output:

00:03  INFO: Connecting: /127.0.0.1:54555
00:03  INFO: [kryonet] Connection 1 connected: /127.0.0.1
00:03  INFO: [CLIENT] You have connected.
BUILD SUCCESSFUL (total time: 3 seconds)

The Server command line Log:

00:00  INFO: [kryonet] Server opened.
00:04 DEBUG: [kryonet] Port 54555/TCP connected to: /127.0.0.1:53217
00:04 DEBUG: [kryo] Write: RegisterTCP
00:04  INFO: [kryonet] Connection 1 connected: /127.0.0.1
00:04  INFO: [SERVER] Someone has connected.
00:04 DEBUG: [kryonet] Connection 1 update: Se ha forzado la interrupcion de una
 conexion existente por el host remoto
00:04  INFO: [SERVER] Someone has disconnected.
00:04  INFO: [kryonet] Connection 1 disconnected.

It seems like the system closes the TCP connection, but i don't really know. Must i enable something in Windows or/and router to permit communication of Kryonet?

Can somebody spot the problem? Thanks in advance.

The line that appears in spanish in the command line log is something like "An interruption of an existant connection has been forced by the remote host."

EDIT after user1816380 advice:

Most of times it still shows the original error, but from time to time you can see:

00:00  INFO: [kryonet] Server opened.
00:07 DEBUG: [kryonet] Port 54555/TCP connected to: /127.0.0.1:50787
00:07 DEBUG: [kryo] Write: RegisterTCP
00:07  INFO: [kryonet] Connection 1 connected: /127.0.0.1
00:07  INFO: [SERVER] Someone has connected.
00:07 DEBUG: [kryo] Read: Packet0LoginRequest
00:07 DEBUG: [kryonet] Connection 1 received TCP: Packet0LoginRequest
00:07 DEBUG: [kryo] Write: Packet1LoginAnswer
00:07 DEBUG: [kryonet] Connection 1 sent TCP: Packet1LoginAnswer (6)
00:07 DEBUG: [kryonet] Connection 1 update: Se ha forzado la interrupcion de una
 conexion existente por el host remoto
00:07  INFO: [SERVER] Someone has disconnected.
00:07  INFO: [kryonet] Connection 1 disconnected.
like image 687
TMichel Avatar asked Jan 15 '23 01:01

TMichel


1 Answers

In order for your client to stay connected, it needs to send and receive KeepAlive packets. When you call client.start(); client.connect(); A client thread is started in the background that automatically handles this for you.

It seems like you're preventing the client thread from doing this because your blocking it with an infinite loop to handle the user input (While(true) get user input).

Instead, you should have a separate thread to take user input. Here's one way(probably not the best) to implement the Client's received function:

public void received(Connection c, Object o) {
    System.out.println("received something");
    if (o instanceof Packet1LoginAnswer){

        boolean answer = ((Packet1LoginAnswer) o).accepted;

        if (answer) {
          System.out.println("Please enter your message for server");
            new Thread("Get User Input") {
                public void run () {
                    try {

                      if (ChatClient.scanner.hasNext()){
                        Packet2Message mpacket = new Packet2Message();
                        mpacket.message = ChatClient.scanner.nextLine();
                        client.sendTCP(mpacket);
                        System.out.println("Please enter another message");
                      }

                    } catch (Exception ex) {
                        ex.printStackTrace();
                        System.exit(1);
                    }
                }
            }.start();

        } else {
              System.out.println("Answer is false");
          c.close();
        }
    }

    if (o instanceof Packet2Message){
        String message = ((Packet2Message) o).message;
        Log.info(message);
    }
}

I also noticed that your using Log.info(). This only works if your using the Debug version of Kryonet. It's better to just use standard output functions.

like image 61
ojaber Avatar answered Jan 24 '23 13:01

ojaber