EsotericSoftware / kryonet

TCP/UDP client/server library for Java, based on Kryo
BSD 3-Clause "New" or "Revised" License
1.82k stars 419 forks source link

IOS connection disconnects immediately #54

Open sanjeev909 opened 10 years ago

sanjeev909 commented 10 years ago

I'm connecting to my kryonet server, from IOS using a simple socket connection API.

It disconnects as the code cannot find udpRemoteAddres

What is udpRemoteAddress, and how can I pass it from the client. From a java client, I can pass the udp port as a second param, but from IOS I see the API allows only one socket address to be passed.

Please help in understanding if I can connect to kryonet using normal socket API's or will I need a kryonet client.

NathanSweet commented 10 years ago

KryoNet uses Kryo for serialization by default, which uses Java class definitions for a schema. It is likely too much work to try to write something for iOS to be able to sue this. You could try using RoboVM to run Java on iOS. Eg, see libgdx.

KryoNet serialization is pluggable, you could use JsonSerialization which is provided.

-Nate

On Wed, Dec 18, 2013 at 3:35 PM, sanjeev909 notifications@github.comwrote:

I'm connecting to my kryonet server, from IOS using a simple socket connection API.

It disconnects as the code cannot find udpRemoteAddres

What is udpRemoteAddress, and how can I pass it from the client. From a java client, I can pass the udp port as a second param, but from IOS I see the API allows only one socket address to be passed.

Please help in understanding if I can connect to kryonet using normal socket API's or will I need a kryonet client.

— Reply to this email directly or view it on GitHubhttps://github.com/EsotericSoftware/kryonet/issues/54 .

sanjeev909 commented 10 years ago

I was planning to use the Kryococoa library for cross-serializing between java and objective-c...the kryococoa project on github talks about mapping objects from java to obj-c and vice-versa

I was hoping that if I could connect to the server using socket library, the serialization can be plugged-in.

sanjeev909 commented 10 years ago

https://github.com/Feuerwerk/kryococoa

NathanSweet commented 10 years ago

Ah ok. KryoNet uses regular sockets, so it should be possible.

Do you need UDP? If not, then don't specify a UDP port when you start your server. If you do, then you need to send a RegisterUDP object from the client to the server which contains the address of UDP communications to/from that client. Probably easiest to just use TCP to get started (probably UDP isn't needed at all). The server sends RegisterTCP immediately after connection, so that will be the first object you need to deserialize.

-Nate

On Wed, Dec 18, 2013 at 4:14 PM, sanjeev909 notifications@github.comwrote:

https://github.com/Feuerwerk/kryococoa

— Reply to this email directly or view it on GitHubhttps://github.com/EsotericSoftware/kryonet/issues/54#issuecomment-30849121 .

sanjeev909 commented 10 years ago

Tried this with a simple java client for faster prototype...but could not get it to work.

    clientSocket = new Socket("127.0.0.1", 54555);
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    Input input = new Input(clientSocket.getInputStream());

    Kryo kryo = new Kryo();
    kryo.register(RegisterTCP.class);

    Object obj = kryo.readObject(input, RegisterTCP.class);

Throws an exception
java.lang.ArrayIndexOutOfBoundsException: -2 at java.util.ArrayList.get(Unknown Source) at com.esotericsoftware.kryo.util.MapReferenceResolver.getReadObject(MapReferenceResolver.java:46) at com.esotericsoftware.kryo.Kryo.readReferenceOrNull(Kryo.java:776) at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:627)

sanjeev909 commented 10 years ago

public Object getReadObject (Class type, int id) { return readObjects.get(id); }

readObjects List has 0 elements...and id = -2

tanis2000 commented 10 years ago

@NathanSweet I'm trying to do something similar, making an Objective-C server that talks to a Java client. But I'm hitting a problem where I don't know the ID with which the RegisterTCP.class is registered with kryo. Is that a fixed number or is it dynamically allocated? And if it's the latter, is there a workaround?

NathanSweet commented 10 years ago

It's a unique number generated by the server. The client gets it from the server so you can do whatever you want. KryoNey just increments an int for each connection.

-Nate

On Mon, Jan 20, 2014 at 4:35 PM, Valerio Santinelli < notifications@github.com> wrote:

@NathanSweet https://github.com/NathanSweet I'm trying to do something similar, making an Objective-C server that talks to a Java client. But I'm hitting a problem where I don't know the ID with which the RegisterTCP.class is registered with kryo. Is that a fixed number or is it dynamically allocated? And if it's the latter, is there a workaround?

— Reply to this email directly or view it on GitHubhttps://github.com/EsotericSoftware/kryonet/issues/54#issuecomment-32769194 .

tanis2000 commented 10 years ago

@NathanSweet that's clear, but what I really need to know is the ID that you give to the RegisterTCP class when you register it with a call like kryo.register(RegisterTCP.class, XXX) :) Thanks!

NathanSweet commented 10 years ago

Ah. Similar story, classes are given an ordinal when registered with Kryo. Depends on the order classes are registered, so they must be registered in the same order on both client and server.

-Nate

On Mon, Jan 20, 2014 at 5:19 PM, Valerio Santinelli < notifications@github.com> wrote:

@NathanSweet https://github.com/NathanSweet that's clear, but what I really need to know is the ID that you give to the RegisterTCP class when you register it with a call like kryo.register(RegisterTCP.class, XXX) :) Thanks!

— Reply to this email directly or view it on GitHubhttps://github.com/EsotericSoftware/kryonet/issues/54#issuecomment-32773410 .

tanis2000 commented 10 years ago

@NathanSweet so, correct me if I'm wrong, I should manually register all the different classes to make sure that they match the way they get registered by the original kryonet?

NathanSweet commented 10 years ago

Yep. You could register the classes with Java and use those IDs in ObjC.

On Mon, Jan 20, 2014 at 7:37 PM, Valerio Santinelli < notifications@github.com> wrote:

@NathanSweet https://github.com/NathanSweet so, correct me if I'm wrong, I should manually register all the different classes to make sure that they match the way they get registered by the original kryonet?

— Reply to this email directly or view it on GitHubhttps://github.com/EsotericSoftware/kryonet/issues/54#issuecomment-32785151 .

tanis2000 commented 10 years ago

It kind of work now. But it looks like the class ID of my own classes do not match. But it might as well be the ObjC deserializer playing tricks on me. I set my class to have ID 100. On the receiver I see that the first byte after the packet length is 0x66. But for some reason the deserializer keeps telling me that the ID of the deserialized class is 78. Kind of weird. Is the packet class ID a single byte btw?

Valerio Santinelli

Inviato da iPhone

Il giorno 20/gen/2014, alle ore 19:57, Nathan Sweet notifications@github.com ha scritto:

Yep. You could register the classes with Java and use those IDs in ObjC.

On Mon, Jan 20, 2014 at 7:37 PM, Valerio Santinelli < notifications@github.com> wrote:

@NathanSweet https://github.com/NathanSweet so, correct me if I'm wrong, I should manually register all the different classes to make sure that they match the way they get registered by the original kryonet?

— Reply to this email directly or view it on GitHubhttps://github.com/EsotericSoftware/kryonet/issues/54#issuecomment-32785151 .

— Reply to this email directly or view it on GitHub.

NathanSweet commented 10 years ago

https://github.com/EsotericSoftware/kryo/blob/master/src/com/esotericsoftware/kryo/util/DefaultClassResolver.java#L84

On Mon, Jan 20, 2014 at 8:41 PM, Valerio Santinelli < notifications@github.com> wrote:

It kind of work now. But it looks like the class ID of my own classes do not match. But it might as well be the ObjC deserializer playing tricks on me. I set my class to have ID 100. On the receiver I see that the first byte after the packet length is 0x66. But for some reason the deserializer keeps telling me that the ID of the deserialized class is 78. Kind of weird. Is the packet class ID a single byte btw?

Valerio Santinelli

Inviato da iPhone

Il giorno 20/gen/2014, alle ore 19:57, Nathan Sweet < notifications@github.com> ha scritto:

Yep. You could register the classes with Java and use those IDs in ObjC.

On Mon, Jan 20, 2014 at 7:37 PM, Valerio Santinelli < notifications@github.com> wrote:

@NathanSweet https://github.com/NathanSweet so, correct me if I'm wrong, I should manually register all the different classes to make sure that they match the way they get registered by the original kryonet?

— Reply to this email directly or view it on GitHub< https://github.com/EsotericSoftware/kryonet/issues/54#issuecomment-32785151>

.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/EsotericSoftware/kryonet/issues/54#issuecomment-32790331 .