Saturn
Saturn

Reputation: 18139

GameKit: sending reliable NSData to other players?

Okay, I managed to create a match between two players. Now I will do a little test about sending data to a player.

I didn't quite get the explanation about NSData. Essentially, what is it? How do I send a, dunno, array to another player?

Apple mentioned data packets could be lost. But there was a "reliable" mode to prevent that. But I couldn't find about such.

Any ideas?

Upvotes: 0

Views: 1069

Answers (1)

Fattie
Fattie

Reputation: 12582

Yes, you have to make your OWN protocol. "making a protocol" is something you do yourself. Something like...Essentially on the server end

// the message arrives and you then do this...
[data getBytes:&getMe length:sizeof(CommsProt)];

whereas on the client end to send messages, you do this...

NSData * data = [NSData dataWithBytes:&sendMe length:sizeof(CommsProt)];
// ...now send that data using GameKit or whatever system you end up with

and you define your protocol - at least the chunks of data - like this:

typedef struct _CommsProt
    {
    BOOL        happyThing;
    someThings  wotJustHappened;
    float       happyValue;
    float       anotherHappyValue;
    // etc
    }
    CommsProt;

If you are new to GK, be aware of this critical tip ...

Client/Server GKSessions

This could also help...

Most effective way to do networking on Mac/iPhone?

Some helpful notes...

(i) "client" and "server" mean nothing. You will be able to send the handbags of information (such as "CommsProt" above) in either direction. If you want to think of, and refer to one, end as a server (particularly if you have a hub-type arrangement), that's fine. But it's only in your head. (By the way, commonly you might use a different data structure in each direction, that's perfectly fine.)

(ii) Regarding sockets. If you get heavily in to networking, you will have to deal with sockets and write your own sockets code. However it is very likely you can choose a networking layer where you never even have to say the word "sockets"! GameKit + Bonjour, for example, completely take care of handling sockets for you, AND that combination takes care of the other incredibly difficult issue which is FINDING one of your client/servers. If you are new I recommend you completely set aside sockets for now, and use a system such as GameKit (or whatever is equivalent on Windows) for your networking layer.

(iii) Indeed AT FIRST just use something incredibly simple like GameKit, while you figure out your protocol and all the other headaches. Later, if necessary you can rewrite the networking layer, or, switch to some other package that you hear about. Happily everything up to the code examples above will be unchanged, only the networking layer will change.

(iv) Regarding WiFi. GameKit and most convenience packages, are completely agnostic to the physical layer: they take care of it for you. GK will work fine however the phones are connected -- bluetooth, ethernet, whatever! Indeed you "won't know" what physical layer is being used. (It's sometimes annoying you can't control this: let it go, users couldn't care less.)

Upvotes: 2

Related Questions