laduramvishnoi / kryonet

Automatically exported from code.google.com/p/kryonet
0 stars 0 forks source link

Kryonet disconnects just after connecting #34

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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!!

Original issue reported on code.google.com by cai...@gmail.com on 7 Jul 2013 at 5:02

Attachments:

GoogleCodeExporter commented 9 years ago
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. 

Original comment by twazerty on 9 Sep 2013 at 7:36

GoogleCodeExporter commented 9 years ago
I also had this problem.

Original comment by renne...@gmail.com on 6 Nov 2013 at 9:46

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Hi, everyone, I think I'v solved this problem. It's a small trick: just set the 
timeout of connection as big as possible (or set it 0 to disable timeout 
detection). Look at the code in Connection.java and TcpConnection.java

-------------------------- Connection.java begin 
-----------------------------------
/** An empty object will be sent if the TCP connection has not sent an object 
within the specified milliseconds. Periodically
     * sending a keep alive ensures that an abnormal close is detected in a reasonable amount of time (see {@link #setTimeout(int)}
     * ). Also, some network hardware will close a TCP connection that ceases to transmit for a period of time (typically 1+
     * minutes). Set to zero to disable. Defaults to 8000. */
    public void setKeepAliveTCP (int keepAliveMillis) {
        tcp.keepAliveMillis = keepAliveMillis;
    }

    /** If the specified amount of time passes without receiving an object over TCP, the connection is considered closed. When a TCP
     * socket is closed normally, the remote end is notified immediately and this timeout is not needed. However, if a socket is
     * closed abnormally (eg, power loss), KryoNet uses this timeout to detect the problem. The timeout should be set higher than
     * the {@link #setKeepAliveTCP(int) TCP keep alive} for the remote end of the connection. The keep alive ensures that the remote
     * end of the connection will be constantly sending objects, and setting the timeout higher than the keep alive allows for
     * network latency. Set to zero to disable. Defaults to 12000. */
    public void setTimeout (int timeoutMillis) {
        tcp.timeoutMillis = timeoutMillis;
    }
--------------------------- Connnection.java end 
----------------------------------

--------------------------- TcpConnection.java begin 
------------------------------
public boolean isTimedOut (long time) {
        return socketChannel != null && timeoutMillis > 0 && time - lastReadTime > timeoutMillis;
    }
--------------------------- TcpConnection.java end 
--------------------------------

The default interval of sending keep-alive message is 8000 ms, so if the 
timeout is close to 8000 ms, the connection is easily to be detected as "timed 
out" because of the network latencies. If the timeout is small, the connection 
is much easier to "break off" if there are multiple clients.

Original comment by tjiay...@gmail.com on 12 Nov 2013 at 6:47

GoogleCodeExporter commented 9 years ago
I don't see how the attached image fixes anything?

@tjiayong, the timeoutMillis on TcpConnection shouldn't be causing a problem 
because lastReadTime and lastWriteTime are set to the current time as soon as a 
connection is connected (on the client) or accepted (on the server).

How can I reproduce this problem?

Also please note that the issues have moved to GitHub:
https://github.com/EsotericSoftware/kryonet/issues/35

Original comment by nathan.s...@gmail.com on 12 Nov 2013 at 7:22

GoogleCodeExporter commented 9 years ago
@nathan, I have to note, my problem is not as the same as the first guy's. I 
deployed my programme on 4 nodes, with 1 server and 3 clients, and the network 
is not timely. If only one client connected to server,it usually works will. 
However, if two or three clients connected to server in succession, the clients 
ahead uausally (not always) disconnnected (even when they haven't send any 
custom message).
I changed the log level to TRACE,and find this is because the server usually 
detecteda timeout on the clients ahead in 
Server.update()->TcpConnection.isTimeout(). The lastReadTime is updated by 
readObject(),which also reads keepalive messages. so I think it should be 
caused by the timeout of keepalive messages because the default keepalive 
interval (8000 ms) and the default timeout of TcpConnection (12000 ms) are too 
close. So I set timeout to 60000 ms, and the problem is solved. 

Original comment by tjiay...@gmail.com on 13 Nov 2013 at 5:34

GoogleCodeExporter commented 9 years ago
So, multiple clients connect at the same time, the server connects 1 or more, 
then spends a long time connecting more clients. The connected clients don't 
hear from the server within the timeout, so they disconnect.

Maybe we can help this by having the server send keep alives as needed between 
every key, not just when it is done with all selected keys. This way multiple 
clients connecting can only stall the server for as long as a single socket 
accept. A similar change was made in Client recently. Committed to github.

Other than that, I think increasing the timeout is ok if your networks is very 
slow.

Original comment by nathan.s...@gmail.com on 13 Nov 2013 at 10:46

GoogleCodeExporter commented 9 years ago
I know this is old and that the project is on github now, but I just wanted to 
say that I also had to do what tijay was doing to make it work using TCP. 
KryoNet 2.21. 

My network is super stable, but I got the immediate disconnect right after a 
connection was made. 

Got the error:
java.net.SocketTimeoutException: Connected, but timed out during TCP 
registration.
 Note: Client#update must be called in a separate thread during connect.
    at com.esotericsoftware.kryonet.Client.connect(Client.java:168)
    at com.esotericsoftware.kryonet.Client.connect(Client.java:111)

By setting the client.connect(tcp) timeout to a high number it solved it. I 
tested with 3 clients connected (2 android phones and 1 pc) to an android 
server, and sending messages worked fine back and forth.

Original comment by simen.s....@gmail.com on 9 Nov 2014 at 1:49