简体   繁体   English

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

[英]Kryonet client disconnects after send a packet to server (java)

I'm doing a little MMO project and right now I'm working on the login/register system. 我正在做一个小型MMO项目,现在正在开发登录/注册系统。 Whenever I try to send a ClientLoginPacket, the client disconnects from the server and the packet is not received at all by the server. 每当我尝试发送ClientLoginPacket时,客户端都会与服务器断开连接,并且服务器根本不会收到该数据包。 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: 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). 与服务器和服务器的唯一区别是导入和包声明(它的archipeloserver而不是仅仅archipelo)。 As you can see, this class extends Packet, here is my Packet class: 如您所见,该类扩展了Packet,这是我的Packet类:

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. 所有数据包都有一个send()方法,该方法被称为发送数据包。 The way I send my packets is by doing this new ClientLoginPacket(username, password).send(); 我发送数据包的方式是执行此new ClientLoginPacket(username, password).send(); . I the ClientLoginPacket class you can see that is runs Networking.sentTCP(this) to send the packet. 在ClientLoginPacket类中,您可以看到正在运行Networking.sentTCP(this)发送数据包。 This just runs this code in my main kryonet class Networking.java . 这只是在我的主kryonet类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. 在kryonet中,您必须先注册课程,然后再发送课程。 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! 提前致谢!

ps sorry for throwing all that code out. ps很抱歉丢掉所有代码。 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. Kryo需要一个没有任何参数反序列化的构造函数。 It looks like your ClientLoginPacket might need one? 看来您的ClientLoginPacket需要一个? 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. 直到我在服务器上使用调试kryonet jar并打开日志,我才得到解释它的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM