将数据包发送到服务器后,Kryonet客户端断开连接(java) [英] Kryonet client disconnects after send a packet to server (java)

查看:504
本文介绍了将数据包发送到服务器后,Kryonet客户端断开连接(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个小型MMO项目,现在我正在开发登录/注册系统.每当我尝试发送ClientLoginPacket时,客户端都会与服务器断开连接,并且服务器根本不会收到该数据包.没有显示堆栈跟踪,但这是我的代码.抱歉,这很多,但这都是必须的:

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:

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);
    }
}

这是登录数据包.与服务器和服务器的唯一区别是导入和包声明(它的archipeloserver而不是仅仅archipelo).如您所见,该类扩展了Packet,这是我的Packet类:

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;
    }

}

所有数据包都有一个send()方法,该方法被称为发送方法.我发送数据包的方式是通过执行new ClientLoginPacket(username, password).send();来完成的.在ClientLoginPacket类中,您可以看到正在运行Networking.sentTCP(this)发送数据包.这只是在我的主kryonet类Networking.java中运行此代码.这是它用于在客户端发送数据包的代码:

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);
}

在kryonet中,您必须先注册课程,然后再发送课程.我做到了,但是我不知道我做得是否正确.这是我使用的确切代码. 服务器:

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.");
}

客户:

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.很抱歉将所有代码都扔掉了.如果没有必要,我通常不会这样做.我尽量减少.如果您需要更多信息来查看其他内容的工作原理以及是否存在问题,请问我.谢谢!

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需要一个没有任何反序列化参数的构造函数.看来您的ClientLoginPacket需要一个?这也给我带来了问题.直到我在服务器上使用调试kryonet jar并打开日志,我才得到解释该错误的错误消息.

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.

这篇关于将数据包发送到服务器后,Kryonet客户端断开连接(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆