java 将数据包发送给,将数据包发送到服务器后,Kryonet客户端断开连接(java)...

I'm doing a little MMO project and right now I'm working on the login/register system. Whenever I try to send a ClientLoginPacket, the client disconnects from the server and the packet is not received at all by the server. There is no stack trace that shows up but here is my code. Sorry it's a lot but it's all necessary:

ClientLoginPacket.java:

package net.vediogames.archipelo.networking.packets;

import net.vediogames.archipelo.networking.Networking;

public class ClientLoginPacket extends Packet{

private String username;

private String password;

private int validity = 0;

public ClientLoginPacket(String username, String password){

this.username = username;

this.password = password;

}

public String getUsername(){

return this.username;

}

public String getPassword(){

return this.password;

}

public int getLoginValidity(){

return validity;

}

public void setLoginValidity(int validity){

this.validity = validity;

}

public void send(){

Networking.sendTCP(this);

}

}

That's the login packet. The only difference with this one and the server one is the import and package declaration (its archipeloserver instead of just archipelo). As you can see, this class extends Packet, here is my Packet class:

package net.vediogames.archipelo.networking.packets;

public abstract class Packet {

protected int connectionID;

public abstract void send();

public int getConnectionId(){

return connectionID;

}

public void setConnectionID(int id){

this.connectionID = id;

}

}

All packets have a send() method that is called to send them. The way I send my packets is by doing this new ClientLoginPacket(username, password).send();. I the ClientLoginPacket class you can see that is runs Networking.sentTCP(this) to send the packet. This just runs this code in my main kryonet class Networking.java. Here is the code it uses to send packets on the client side:

public static void sendTCP(Packet object){

client.sendTCP(object);

}

In kryonet, you have to register classes before sending them. I did that but I don't know if I did it properly. Here is the exact code I used.

Server:

private static void setupClasses(){

Kryo kryo = server.getKryo();

kryo.register(ClientRegisterPacket.class);

kryo.register(ClientLoginPacket.class);

System.out.println("Registered classes.");

}

Client:

public static void setupClasses(){

Kryo kryo = client.getKryo();

kryo.register(ClientRegisterPacket.class);

kryo.register(ClientLoginPacket.class);

}

What I know for sure is that I do have a connection to the server before sending a packet, I tested it with the connection listener on the server. What would my issue be? Is there something wrong with my class registration? Do both classes have to be completely identical? Thanks in advance!

p.s. sorry for throwing all that code out. I wouldn't normally do this if I didn't have to. I put the least as possible. If you need more to see how the other stuff works and to see if the issue is there, just ask me. Thanks!

解决方案

Kryo needs a constructor without any arguments to deserialize. It looks like your ClientLoginPacket might need one? This caused an issue for me as well. It wasn't until I used the debug kryonet jars on the server and turned logging on that I got the error message that explained it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值