Kryonet shuts down immediately after connecting

I followed this Youtube tutorial , which covers the basics of Cryonet.

Basically this is Kryonet's Hello World, it explains how to configure the base server and client, allowing the client to send packets to the server and have a very basic connection.

Link to the source code . Both server and client have the same package class.

I can start the server, and the Client requests an IP connection. However, when I enter IP, the Client ends immediately 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) 

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 that the system is closing the TCP connection, but I really don't know. Do I have to enable something on a Windows or / or router to allow Kryonet communication?

Can anyone spot the problem? Thanks in advance.

The line displayed in Spanish in the command line log is something like "Terminating an existing connection was forcibly deleted using the remote host."

EDIT after user 1816380:

In most cases, 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. 
+4
source share
5 answers

For your client to stay connected, they must send and receive KeepAlive packets. When you call client.start (); client.connect (); A client thread runs in the background, which automatically processes this for you.

It seems that you are preventing the client thread from doing this because you are blocking it with an infinite loop to process user input (While (true) get user input).

Instead, you should have a separate stream for user input. Here is one way (perhaps not the best) for implementing a client 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 you are using Log.info (). This only works if you are using the debug version of Kryonet. It's better to just use standard output functions.

+6
source

I had the same problem, I tried all the other answers, but they did not work. It turned out that I had a server on TCP port 1941 and UDP port 1942 however, my client was connecting to a server with TCP port 1941 (ignoring the UDP port). Adding a UDP port like this solved this for me:

 client.connect(5000, address, 1941, 1942); 
+2
source

The same problem and behavior may be related to using the latest version of KryoNet 2.20. When using kryonet-2.12, for example, the problem will most likely be resolved.

So try using kryonet-2.12 instead of kryonet-2.20, and the example should work; -)

0
source

I'm not sure if this is relevant (since your link to the source code no longer works), but ...

The problem you are describing may occur if you have configured your server for TCP and UDP, but then only connect your client through TCP.

If you want to use the host discovery, but after that you only need a TCP connection, then you are advised to "start a separate server for UDP discovery" .

0
source

I think you are not running the client in a separate thread.

"Starting with r122, client update threads were made into daemon threads, causing child processes to close as soon as they complete initialization." Solution "Perhaps you could use this new theme (client) .start ();".

So let's say, for example, you start your client as follows

 client.start(); 

rather you should use

 new Thread(client).start(); 
0
source

Source: https://habr.com/ru/post/1445239/


All Articles