I'm trying to set up a basic client and server to get the hang of networking, but I'm running into a problem.
Whenever I send a class with one of my own classes as a variable, the server does not receive it. I can use a string/int/etc for the variable perfectly fine but whenever a variable of my own type is included it is not received, along with all objects I try to send after it.
Everything is registered with Kryo. Am I just missing something? Any help is appreciated!
From GameClient:
public GameClient(String name) {
client = new Client();
client.start();
NetworkManager.register(client);
listener = new ClientListener();
client.addListener(listener);
try {
client.connect(5000, "24.207.67.56", NetworkManager.port);
} catch (Exception e) {
e.printStackTrace();
}
Login login2 = new Login();
User user2 = new User(name);
login2.user = user2;
login2.name = user2.name;
client.sendTCP(login2); // server does not recieve this
Login login = new Login();
login.name = user.name;
client.sendTCP(login); // server receives this only when it is sent before the Login containing the User
}
From GameServer :
public GameServer() {
server = new Server();
NetworkManager.register(server);
listener = new ServerListener(server);
server.addListener(listener);
try {
server.bind(NetworkManager.port);
} catch (IOException e) {
e.printStackTrace();
}
server.start();
}
In ServerListener:
@Override
public void received(Connection c, Object o) {
System.out.println("recieved");
if (o instanceof Login) {
System.out.println("[SERVER] " + ((Login) o).name + " logged in.");
LoginResult lr = new LoginResult();
lr.result = true;
c.sendTCP(lr);
}
}
From NetworkManager:
public static void register(EndPoint endPoint) {
Kryo kryo = endPoint.getKryo();
kryo.register(Login.class);
kryo.register(User.class);
}
public static class Login {
public User user;
public String name;
}
User:
public class User {
public String name;
public User(String name) {
this.name = name;
}
}
So it turns out that the problem was caused by the lack of default constructor in the User class. Removing the User(String name)
constructor or adding a plain User()
constructor fixes the issue.
It would be useful if it threw an error when you tried to do it incorrectly, but oh well.