Kyronet & androidstudio - timed out during TCP registration

922 Views Asked by At

I'm trying to connect to my server created in kryonet. So running server in eclipse and then connecting in android studio.

I get this weird error.

Server:

package server;


import java.io.IOException;

import com.badlogic.gdx.ApplicationAdapter;
import com.esotericsoftware.kryo.Kryo;
//import com.esotericsoftware.kryonet.KryoSerialization;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import server.SomeRequest;
import server.SomeResponse;
import common.Network;

public class GameServer extends ApplicationAdapter {

static int SERVER_TCP_PORT;
static int SERVER_UDP_PORT;

Server server;

public GameServer () throws IOException {


    server = new Server();
    server.start();

    Kryo kryo = server.getKryo();
    kryo.register(SomeRequest.class);
    kryo.register(SomeResponse.class);

    //server.bind(54555, 54777);
    server.bind(54557);
    System.out.println("Started server!");
    server.addListener(new Listener() {
        public void received (Connection connection, Object object) {
           if (object instanceof SomeRequest) {
              SomeRequest request = (SomeRequest)object;
              System.out.println(request.text);

              SomeResponse response = new SomeResponse();
              response.text = "Thanks";
              connection.sendTCP(response);
           }
        }
    });
}



public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    //SERVER_TCP_PORT = Network.SERVER_TCP_PORT;
    //SERVER_UDP_PORT = Network.SERVER_UDP_PORT;

    new GameServer();
}
}

Client:

package com.mygdx.game.android;

import android.util.Log;

import com.badlogic.gdx.Game;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

import java.io.IOException;
import java.net.InetAddress;

/**
 * Created by 112944 on 25.1.2016.
 */
public class Connect extends Game{

private SomeRequest request;
private SomeResponse response;
private Client client;

private String ipAddress;
private int tcpPort, udpPort;

@Override
public void create() {
    connectToServer();

}
public void render() {
    super.render(); //important!
}
public void dispose() {

}
public void connectToServer(){

    client = new Client();
    client.start();

    InetAddress address = client.discoverHost(54777, 5000);
    Log.w("address", String.valueOf(address));

    Kryo kryo = client.getKryo();
    kryo.register(SomeRequest.class);
    kryo.register(SomeResponse.class);


    Log.w("here", "yeah");
    try {
        //client.connect(50000, "10.0.2.2", 54557, 54777);
        client.connect(5000, "10.0.2.2", 54557);// 10.0.2.2 is addres for connecting localhost from emulator.
    }catch (IOException e){
        Log.w("expection",e);

    }
    request = new SomeRequest();
    request.text = "Here is the request";
    client.sendTCP(request);

    client.addListener(new Listener() {
        public void received(Connection connection, Object object) {
            if (object instanceof SomeResponse) {
                response = (SomeResponse) object;
                Log.w("response",response.text);
            }
        }
    });
}
}

Error:

01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection: java.net.SocketTimeoutException: Connected, but timed out during TCP registration.
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection: Note: Client#update must be called in a separate thread during connect.
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection:     at com.esotericsoftware.kryonet.Client.connect(Client.java:161)
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection:     at com.esotericsoftware.kryonet.Client.connect(Client.java:104)
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection:     at com.mygdx.game.android.Connect.connectToServer(Connect.java:53)
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection:     at com.mygdx.game.android.Connect.create(Connect.java:28)
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection:     at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:243)
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection:     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519)
01-25 14:12:56.986 25060-25083/com.mygdx.game.android W/expection:     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
01-25 14:14:50.531 25060-25066/com.mygdx.game.android W/art: Suspending all threads took: 6.564ms

I've been trying to do everything with no luck.

depencies:

kryo 3.0.3 kryonet 2.21 objenesis-2.1 reflectasm-1.10.1-shaded minlog-1.3.0 gdx-1.7.1

1

There are 1 best solutions below

0
On

As far as I know, you need to connect the client in different thread

I edit your connectToServer method. try it

public void connectToServer(){

client = new Client();
client.start();

InetAddress address = client.discoverHost(54777, 5000);
Log.w("address", String.valueOf(address));

Kryo kryo = client.getKryo();
kryo.register(SomeRequest.class);
kryo.register(SomeResponse.class);

request = new SomeRequest();
request.text = "Here is the request";    

client.addListener(new Listener() {
    public void received(Connection connection, Object object) {
        if (object instanceof SomeResponse) {
            response = (SomeResponse) object;
            Log.w("response",response.text);
        }
    }
});

new Thread(){
    public void run(){
        Log.w("here", "yeah");
        try {
            //client.connect(50000, "10.0.2.2", 54557, 54777);
            client.connect(5000, "10.0.2.2", 54557);// 10.0.2.2 is addres for connecting localhost from emulator.
        }catch (IOException e){
            Log.w("expection",e);

        }
    }
}.start();        

client.sendTCP(request);}