EsotericSoftware / kryonet

TCP/UDP client/server library for Java, based on Kryo
BSD 3-Clause "New" or "Revised" License
1.82k stars 419 forks source link

Kryonet disconnects just after connecting #35

Open ghost opened 10 years ago

ghost commented 10 years ago

From cai...@gmail.com on July 07, 2013 19:02:06

What steps will reproduce the problem?

  1. start a server
  2. start client
  3. client keep sendTCP to server
  4. server not send anything to client What is the expected output? What do you see instead? [kryonet] Connection 1 connected: /127.0.0.1 [kryonet] Connection 1 disconnected. in 12 seconds What version of the product are you using? On what operating system? 2.20 Please provide any additional information below. Reason and how to fixed it: if i am keep writing lastWriteTime will bigger than the parameter "time" for method: com.esotericsoftware.kryonet.TcpConnection.needsKeepAlive(long time); so this will case "time - lastWriteTime" < 0 and of course made it < keepAliveMillis. That is why needsKeepAlive() will always return true, and client killed it by itself. i fixed by adding such code in the needsKeepAlive() method and isTimedOut (long time) method, you can see details in attachment. Hope this helpful!! BTW your frame is great! and simple to use!!

Attachment: compare.jpg

Original issue: http://code.google.com/p/kryonet/issues/detail?id=34

ghost commented 10 years ago

From twazerty on September 09, 2013 12:36:43

I have the same problem when I only connect via TCP (Server listens on TCP and UDP). When I connect with TCP and UDP in de client everything works ok.

ghost commented 10 years ago

From renne...@gmail.com on November 06, 2013 01:46:15

I also had this problem.

Kronos87 commented 10 years ago

I got the same problem. Is there a solution for this problem?

william-reed commented 10 years ago

I am also having this problem, any solution found? @Kronos87

NathanSweet commented 10 years ago

See discussion here: https://code.google.com/p/kryonet/issues/detail?id=34 Please continue any further discussion in this thread though.

CirceanStudios commented 10 years ago

Hey guys, just wanted to add that we're seeing this behavior in the development of an Android app that utilizes Kryonet - when connecting the client only via TCP, it establishes a connection, but any sendTCP calls don't result in the server receiving any data. The server's connected() is not invoked, and the client shortly disconnects after not receiving any response from the server. We are using discoverHost as the basis for the host address the client uses.

If we update the client connect() call to include the UDP port, everything works as expected. The workaround seems to be having the client specify both TCP and UDP ports in connect(), though I do not know the specifics as to what is causing this internally within the Kryonet source (we're using the latest 2.21-all jar in our project).

Just wanted to add our experience, and that the above workaround appears to be working in our case - I hope this helps.

ManuelRauber commented 10 years ago

I experience the same problem and the same solution. You have to specify the UDP and TCP port. Please fix that problem!

NathanSweet commented 10 years ago

No one shows an executable example and all the KryoNet tests pass, so I assume your code is wrong. Guessing at your problem is usually a waste of time, but probably you start a server that expects both a TCP and UDP connection from a client.

ManuelRauber commented 10 years ago

I'll prepare an example next week.

NathanSweet commented 10 years ago

If your server has a TCP and UDP port, your clients must also. You can run a separate server for UDP discovery.

makerimages commented 10 years ago

Also,when having both ports fail, all of your classes that you register with Kryo, should have an empty constructor inside them, fixed the problem for me. Might be worth putting this into the readme, aswell :) , otherwize this thing is :+1:

ManuelRauber commented 10 years ago

I'm sorry I forget this issue here.

@NathanSweet Yes, that was the solution! I needed to specify both ports for the clients, too! Thanks :)

breskeby commented 8 years ago

hit the same issue. my problem was that I registered the UUID class which doesn't provide a public default constructor. replacing it solved the problem for me

ghost commented 8 years ago

@breskeby Exact same problem here. I'd thought I'd added default constructors to all registered classes, but there was one without. Adding a default constructor fixed it immediately.

Isfirs commented 8 years ago

I agree with @MarkSill, I faced the same problem yesterday and adding ALL default constructors fixed everything! Someone should spread this around.

adudez96 commented 6 years ago

@Isfirs Same issue, same solution. Public default constructors need to be added to every single one.

NathanSweet commented 6 years ago

Sounds like two separate problems:

  1. Not specifying both the same ports on client and server (TCP or TCP+UDP), and
  2. Not providing default constructors.

What error does 2 provide? It should be explicit that a zero arg constructor is needed. Does it fail silently instead? If so, are you using StdInstantiatorStrategy? Using that is not recommended (it creates objects without calling any constructor).

KryoNet is due for an update to the latest Kryo, that may help.

Zhuinden commented 5 years ago

My problem was multi-fold...

1.) initially I was using Kotlin data class, which doesn't create empty constructor out of the box

2.) Kotlin also wraps arrays and stuff with its own primitives, so I moved the kryo.register calls (and the commands to send) over to Java

3.) I didn't specify UDP port for the client even though I should have.

4.) I was trying to send TCP message back to the server using the Connection I received in the connected event callback, but apparently instead of that, I was supposed to use client.sendTCP instead of connection.sendTCP.

ghost commented 2 years ago

@breskeby Exact same problem here. I'd thought I'd added default constructors to all registered classes, but there was one without. Adding a default constructor fixed it immediately.

Had the same issue. This solved it immediately.