[英]Connecting client to server Kryonet and Libgdx
I'm trying to connect from a client which uses libgdx for the interface to a server. 我正在尝试从使用libgdx作为接口的客户端连接到服务器。 I use Kryonet on both the client and the server.
我在客户端和服务器上都使用Kryonet。 However it is not working.
但是,它不起作用。 Any idea what's wrong?
知道有什么问题吗?
Here is the code that i use in the client: 这是我在客户端中使用的代码:
Client client = new Client();
Kryo kryo = client.getKryo();
kryo.register(String.class);
client.start();
new Thread() {
public void run() {
try {
client.connect(5000, "localhost", 54555, 54777);
client.addListener(new Listener() {
public void received(Connection connection, Object object) {
if (object instanceof String) {
String response = (String) object;
System.out.println(response);
}
}
});
} catch (IOException e) {
Log.warn("expection", e);
}
}
}.start();
client.sendTCP("Hey There");
Here is the server code: 这是服务器代码:
Server server = new Server();
Kryo kryo = server.getKryo();
kryo.register(String.class);
server.start();
try {
server.bind(54555, 54777);
} catch (IOException e) {
e.printStackTrace();
}
server.addListener(new Listener() {
public void received (Connection connection, Object object) {
if (object instanceof String) {
String request = (String)object;
System.out.println("Server"+request);
String response = "Login succes";
connection.sendTCP(response);
}
}
});
Here is the error that i get: 这是我得到的错误:
[kryonet] Error updating connection.
com.esotericsoftware.kryonet.KryoNetException: Incorrect number of bytes (1 remaining) used to deserialize object: null
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:146)
at com.esotericsoftware.kryonet.Client.update(Client.java:255)
at com.esotericsoftware.kryonet.Client.run(Client.java:338)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Incorrect number of bytes (1 remaining) used to deserialize object: null
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:146)
at com.esotericsoftware.kryonet.Client.update(Client.java:255)
at com.esotericsoftware.kryonet.Client.run(Client.java:338)
at java.lang.Thread.run(Unknown Source)
00:05 WARN: expection
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:117)
at me.fort.historywars.LoginScreen$1.run(LoginScreen.java:37)
Alright, I consider this code fixed: 好的,我认为此代码已修复:
Server: 服务器:
server = new Server();
Kryo kryo = server.getKryo();
kryo.register(SRq.class);
server.start();
try {
server.bind(54555, 54777);
} catch (Exception e) {
System.err.println("Failed to bind to port!");
}
server.addListener(new Listener() {
@Override
public void received(Connection connection, Object object) {
if(object instanceof SRq) {
System.out.println("Server " + ((SRq) object).data);
SRq sRq = new SRq();
sRq.data = "Data";
connection.sendTCP(sRq);
}
}
});
Client: 客户:
client = new Client();
Kryo kryo = client.getKryo();
kryo.register(SRq.class);
client.start();
try {
client.connect(6000, "localhost", 54555, 54777);
} catch (Exception e) {
System.err.println("Failed to connect to server!");
}
client.addListener(new Listener() {
@Override
public void received(Connection connection, Object object) {
if(object instanceof SRq) {
Gdx.app.log("Client", ((SRq) object).data);
}
}
});
System.out.println("Connected to server!");
SRq sRq = new SRq();
sRq.data = "Log in";
client.sendTCP(sRq);
Things to keep in mind: 注意事项:
SRq.class
is just a class with a public String
inside it SRq.class
只是其中包含公共String
的类 kryonet-2.21-all
. kryonet-2.21-all
。 Download the jars, make a folder called libs
in your core folder, put the jar inside it, and add this to your build.gradle
(in core dependencies): compile fileTree(dir: 'libs', include: '*.jar')
libs
文件夹,将jar放入其中,然后将其添加到build.gradle
(在核心依赖项中): compile fileTree(dir: 'libs', include: '*.jar')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.