EsotericSoftware / kryonet

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

java.net.SocketTimeoutException: Connected, but timed out during TCP registration #132

Open Kuutio opened 7 years ago

Kuutio commented 7 years ago

server runs fine. client cant connect. it also says Note: Client#update must be called in a separate thread during connect. But i got new thread for it ? sorry maybe im idiot :D oh it crashes after client.connect(9999, ipAddress, tcpPort, 54777); `public class ConnectClient {

private SomeRequest request;
private SomeResponse response;
private Client client;
public NetworkDATA network;
private String name;

private String ipAddress;
private int tcpPort, udpPort;

private MMORPG game;

public ConnectClient(MMORPG game){
    this(NetworkDATA.SERVER_IP, NetworkDATA.SERVER_TCP_PORT, game);

}
public ConnectClient(String ipAddress, int tcpPort, MMORPG game) {

    client = new Client();

    this.ipAddress = ipAddress;
    this.tcpPort = tcpPort;
    //this.udpPort = udpPort;
    this.game = game;

   network.register(client);

}

public void runClient() {
    Log.w("client started", "started");
    // For consistency, the classes to be sent over the network are
    // registered by the same method for both the client and server.
    client.addListener(new Listener() {
        public void connected(Connection connection) {
            Log.w("connected", "connected");
            NetworkDATA.RegisterName registerName = new NetworkDATA.RegisterName();
            registerName.name = name;
            client.sendTCP(registerName);
        }
    });
    client.start();
    new Thread("Connect") {
        public void run() {
            try {
                Log.w("connecting", "zzz");
                client.connect(9999, ipAddress, tcpPort, 54777); //CRASH HERE
                // Server communication after connection can go here, or in Listener#connected().
            } catch (IOException ex) {
                ex.printStackTrace();
                Log.w("CANT CONNECT", "ERROR");
                System.exit(1);
            }
        }
    }.start();
}

}`

desertkun commented 7 years ago

How do you run corresponding server? Possibly your tcp ports simple don't match.

Ivan-Monahhov commented 4 years ago

This class instance is created on application load. Important note that this is not replicatable with android emulator only with a real phone ( Redmi Go which has Android 8) , but works on CAT S60 which has android 6.0.1

`package org.individual.ivan.geotest;

import com.esotericsoftware.kryonet.Client; import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Listener;

import java.io.IOException; import java.util.Timer; import java.util.TimerTask;

public class ServerConnection extends Listener { Client client; String ip = "188.166.30.219"; int port = 9001; MainActivity activity; public ServerConnection (MainActivity main) { activity = main; this.client = new Client(8192,4096); client.addListener(this); client.getKryo().register(Loc.class); client.start();

    new Thread(){
        public void run()
        {
            try
            {
                client.update(11000);
            }catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }.start();
    try
    {
        client.connect(10000, ip, port);
    }catch (IOException e)
    {
        e.printStackTrace();
        reConnectIn60();
    }
}
public void send(Loc message)
{
    client.sendTCP(message);
}
public void received(Connection c, Object p)
{
    if(p instanceof Loc) {
        Loc loc = (Loc) p;
        activity.updateDistance(loc);
    }
}
private void reConnectIn60()
{
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            new Thread(){
                public void run()
                {
                    try
                    {
                        client.update(11000);
                    }catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }.start();
            try
            {
                client.connect(10000,ip,port);
            } catch (IOException e)
            {
                reConnectIn60();
                e.printStackTrace();
            }

        }
    }, 60000);
}
public void disconnected(Connection c)
{
    reConnectIn60();
}

} `