Reputation: 4442
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.
Upvotes: 4
Views: 4661
Reputation: 868
I just had the same issue, I tried all the other answers but they didn't work.
Turned out I was hosting a server on TCP port 1941
and UDP port 1942
My client however was connecting to a server with TCP port 1941
(ignoring the UDP port). Adding the UDP port like this solved it for me:
client.connect(5000, address, 1941, 1942);
Upvotes: 2
Reputation: 3737
I think you are not starting the client in a separate thread.
"Starting with r122, client update threads were made into daemon threads, causing the child processes to close as soon as they finish initializing.", the solution is "Maybe you could use this? new Thread(client).start();".
So lets say for example you are starting your client like this
client.start();
rather you should use
new Thread(client).start();
Upvotes: 1
Reputation: 2026
I'm not sure if this is relevant (as your link to your source code is no longer working) but...
The problem you are describing can arise if you configure your server for TCP and UDP but then have your client only connect via TCP.
If you're wanting to take advantage of the host discovery but only need a TCP connection thereafter then it is advisable that you "run a separate server for UDP discovery".
Upvotes: 0
Reputation: 10595
This very same problem and behavior can be due to using the latest version of KryoNet 2.20. When using the kryonet-2.12 for example the problem will be most likely solved.
So try to use kryonet-2.12 instead of kryonet-2.20 and the example should work ;-)
Upvotes: 0
Reputation: 61
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.
Upvotes: 6