从客户端收到对象后,我遇到Kryonet服务器崩溃的问题。 服务器代码:
package com.qookie.miner_server;
import java.io.IOException;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;
import com.qookie.miner_server.Packet.*;
public class MinerServer_Main {
private Server server;
public MinerServer_Main() throws IOException {
this.server = new Server();
RegisterPackets();
server.addListener(new NetworkListener());
server.bind(8888,8888);
server.start();
}
private void RegisterPackets() {
Kryo kryo = server.getKryo();
kryo.register(Packet0LoginRequest.class);
kryo.register(Packet1LoginAnswer.class);
kryo.register(Packet2Message.class);
}
public static void main(String[] args) {
try {
new MinerServer_Main();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是一个服务器监听器:
package com.qookie.miner_server;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.qookie.miner_server.Packet.*;
public class NetworkListener extends Listener {
public void connected(Connection arg0) {
System.out.println("[SERVER] Someone has connected");
}
public void disconnected(Connection arg0) {
System.out.println("[SERVER] Someone has disconnected");
}
public void received(Connection c, Object o) {
if (o instanceof Packet0LoginRequest) {
Packet1LoginAnswer p = new Packet().new Packet1LoginAnswer();
p.accepted = true;
c.sendTCP(p);
}
if (o instanceof Packet2Message) {
String message = ((Packet2Message) o).msg;
System.out.println("[CLIENT] " + message);
}
}
}
这是Packet.java文件:
package com.qookie.miner_server;
public class Packet {
public class Packet0LoginRequest {
public Packet0LoginRequest() {}
public void init() {
}
};
public class Packet1LoginAnswer {
public boolean accepted;
public Packet1LoginAnswer() {}
public void init() {
}
};
public class Packet2Message {
public Packet2Message() {}
public String msg;
public void init() {
}
};
public Packet() {
}
}
当用户连接客户端时,发送Packet0LoginRequest并且服务器发送回Packet1LoginAnswer。 当Packet1LoginAnswer中的布尔变量为true时,客户端开始从扫描程序读取并发送新的Packet2Message。 但是在接收Packet0LoginRequest时服务器崩溃了。
以下是崩溃日志: Server crash log
答案 0 :(得分:1)
它认为kryonet
无法加载您的构造函数。
以下方法是我的工作:
public class Network {
static public final int tcpPort = 54555;
static public final int udpPort = 54777;
// This registers objects that are going to be sent over the network.
static public void register (EndPoint endPoint) {
Kryo kryo = endPoint.getKryo();
kryo.register(ServerResponse.class);
kryo.register(ClientRequest.class);
}
static public class ServerResponse {
...
public ServerResponse(){
super();
}
...
}
static public class ClientRequest {
...
public ClientRequest(){
super();
}
...
}
...
}