• Kryonet client disconnects after send a packet to server (java)


    http://stackoverflow.com/questions/25934876/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. 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!

    1 Answer

    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.

    ——————————————————————————————————————————————————————————————————————————

    简单来说,就是kryo乔一个无参构造函数来反序列化,否则就会丢失连接而无其他任何出错信息。

  • 相关阅读:
    类与对象
    类的声明与实例化
    面向对象的基本概念
    css下拉导航栏代码
    面向对象的三大特性
    面向对象三大基本特性,五大基本原则
    dom事件
    PHP 流程
    权限 查找
    留言板案例
  • 原文地址:https://www.cnblogs.com/cuizhf/p/5655193.html
Copyright © 2020-2023  润新知