kryonet 服务器在接收对象时崩溃
kryonet server crashing when receiving an object
我遇到 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 中的布尔变量为真时,客户端开始从扫描器读取并发送新的 Packet2Message。
但是服务器在收到 Packet0LoginRequest 时崩溃了。
这是崩溃日志:
Server crash log
它认为 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();
}
...
}
...
}
我遇到 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 中的布尔变量为真时,客户端开始从扫描器读取并发送新的 Packet2Message。 但是服务器在收到 Packet0LoginRequest 时崩溃了。
这是崩溃日志: Server crash log
它认为 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();
}
...
}
...
}