我正在尝试连接到在kryonet中创建的服务器。所以在eclipse中运行服务器然后在android studio中连接。
我得到了这个奇怪的错误。
服务器
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();
}
}
客户:
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);
}
}
});
}
}
错误:
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
我一直试图做任何事都没有运气。
depencies:
kryo 3.0.3 kryonet 2.21 objenesis-2.1 reflectasm-1.10.1阴影 minlog-1.3.0 GDX-1.7.1
答案 0 :(得分:1)
据我所知,您需要在不同的线程中连接客户端
我编辑你的connectToServer方法。试试吧
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);}