开发者

Java Kryonet [Class is not registered Exception ]

I found this API called Kryonet. Well, i tried to implement the example provided in the project page. However, it wasn't successful.

Server code:

public class KryoTest {

public KryoTest() throws IOException {
    Server server = new Server();
    server.start();
    server.bind(54555, 54777);
    server.addListener(new Listener() {

        public void received(Connection connection, Object object) {
            if (object instanceof SomeRequest) {
                SomeRequest request = (SomeRequest) object;
                System.out.println(request.text);

                SomeResponse response = new SomeResponse();
                response.text = "Thanks!";
                connection.sendTCP(response);
            }
        }
    });
    Kryo kryo = server.getKryo();
    kryo.register(SomeRequest.class);
    kryo.register(SomeResponse.class);
}

public static void main(String[] args) throws IOException {
    new KryoTest();
}}

Client Code:

    public class Kryoclient {

    public Kryoclient() throws IOException {
    Client client = new Client();
client.start();
client.connect(5000,"192.168.1.4", 54555, 54777);

SomeRequest request = new SomeRequest();
request.text = "Here is the reque开发者_StackOverflowst!";
client.sendTCP(request);

 Kryo kryo = client.getKryo();
kryo.register(SomeRequest.class);
kryo.register(SomeResponse.class);

}
    public static void main(String[] args) throws IOException {
        new Kryoclient();
    }
}

Exception:

run:
00:00  INFO: Connecting: /192.168.1.4:54555/54777
00:00  INFO: [kryonet] Connection 1 connected: /192.168.1.4
Exception in thread "main" java.lang.IllegalArgumentException: Class is not registered: client.SomeRequest
    at com.esotericsoftware.kryo.Kryo.getRegisteredClass(Kryo.java:319)
    at com.esotericsoftware.kryo.Kryo.writeClass(Kryo.java:374)
    at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:484)
    at com.esotericsoftware.kryonet.TcpConnection.send(TcpConnection.java:196)
    at com.esotericsoftware.kryonet.Connection.sendTCP(Connection.java:68)
    at client.Kryoclient.<init>(Kryoclient.java:24)
    at client.Kryoclient.main(Kryoclient.java:30)

What is wrong with this code?


I've never heard of Kryonet before now, but I'd assume you'll need to move your kryo.register(...) lines to before the client or the server first tries to connect or accept a connection, respectively.


Kryoserver is a very good multi-threading client-server manager.

You have to register all classes that are sent and received on both the Client and the Server (in same order), before you connect your client and start or bind to your server.


Code order should be this:

//Create server instance

Server server = new server();

//Create Kryo instance from server instance and register classes you gonna send through network.

Kryo kryo = server.getKryo();

kryo.register(ClassNameYouWant.class);

//Add listeners

server.addListener( ... );

//Bind port

server.bind(4345);

//And only now start the server.

server.start();

In your code you have started the server before registering classes and adding listeners.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜