将客户端连接到服务器 Kryonet 和 Libgdx
Connecting client to server Kryonet and Libgdx
我正在尝试从使用 libgdx 作为接口的客户端连接到服务器。我在客户端和服务器上都使用 Kryonet。但是它不起作用。知道出了什么问题吗?
这是我在客户端中使用的代码:
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");
这是服务器代码:
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);
}
}
});
这是我得到的错误:
[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.run(LoginScreen.java:37)
好的,我认为此代码已修复:
服务器:
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 = 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);
注意事项:
SRq.class
只是一个 class 里面有一个 public String
- 确保您在客户端和服务器中使用相同的 KryoNet 版本。我用了
kryonet-2.21-all
。下载 jars,在核心文件夹中创建一个名为 libs
的文件夹,将 jar 放入其中,并将其添加到 build.gradle
(在核心依赖项中):compile fileTree(dir: 'libs', include: '*.jar')
我正在尝试从使用 libgdx 作为接口的客户端连接到服务器。我在客户端和服务器上都使用 Kryonet。但是它不起作用。知道出了什么问题吗?
这是我在客户端中使用的代码:
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");
这是服务器代码:
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);
}
}
});
这是我得到的错误:
[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.run(LoginScreen.java:37)
好的,我认为此代码已修复:
服务器:
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 = 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);
注意事项:
SRq.class
只是一个 class 里面有一个 publicString
- 确保您在客户端和服务器中使用相同的 KryoNet 版本。我用了
kryonet-2.21-all
。下载 jars,在核心文件夹中创建一个名为libs
的文件夹,将 jar 放入其中,并将其添加到build.gradle
(在核心依赖项中):compile fileTree(dir: 'libs', include: '*.jar')