开发者

Sending C Strings contained in structs through game center

I'm trying to send a message with game center that contains a struct, roughly following Ray Wenderlich's guide. In general sending and receiving messages has been no problem, but once I throw anything with a pointer in I get a bit confused. The goal is to send a few strings and a BOOL over as one message. Relevant code follows:

//myfile.h
typedef enum {
    kMessageTypeRandomNumber = 0,
    kMessageTypeInterstitial
} MessageType;
typedef struct {
    MessageType messageType;
} Message;
typedef struct {
    Message message;
    char *playerName;
    char *scores;
    BOOL correct;
    char *stat;
} MessageInterstitial;


//myfile.m
- (void)sendData:(NSData *)data {
    NSError *error;
    BOOL success = [[GCHelper sharedInstance].match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
    if (!success) {
        NSLog(@"Error sending init packet:\n%@",error);
    }
}
-(void)send{
    MessageInterstitial message;
    message.message.messageType = kMessageTypeInterstitial;
    message.playerName="test";
    NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageInterstitial)];    
    [self sendData:data]; 
    MessageInterstitial * myMessage = (MessageInterstitial *) [data bytes];
    printf("player: %s\n",myMessage->playerName); // prints 'player: test' as ex开发者_如何学编程pected
}
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
    Message *message = (Message *) [data bytes];
    if(message->messageType == kMessageTypeInterstitial)
    {
        MessageInterstitial * myMessage = (MessageInterstitial *) [data bytes];
        printf("player: %s\n",myMessage->playerName); //prints 'player: ' not expected
    }
}

It appears that the data is being saved properly, since I'm able to print it back out right away without issue, but my thinking is that the data is pointing to an address in memory, which would make it so it prints out the correct thing on the sending device, but not the receiving device. That being said, I've got no clue how to fix this, or how i should be sending strings within a struct if that is in fact the case.


Indeed this seems like the problem, the addresses are addresses in the sending device, not receiving device. You can use char arrays instead, and just allocate enough space from the beginning. This way they will be part of the struct:

// those are just examples of course...
typedef struct {
    Message message;
    char playerName[30];
    char scores[30];
    BOOL correct;
    char stat[15];
} MessageInterstitial;


You have to use a char array, but one thing is that when you create your struct, this char array is randomly filled with all sorts of weird characters, so here's how got rid of those characters :

typedef struct {
char stringToSend[20];
int stringToSendLength;
} myStruct;

// create a Struct and an NSString
myStruct aStruct;
NSString *stringToConvert = @"stringToConvert";

// convert NSString into char array
for (int i = 0; i < stringToConvert.length; i++) {
    aStruct.stringToSend[i] = [stringToConvert characterAtIndex:i];
}

aStruct.stringToSendLength = stringToConvert.length; // send string length

// store struct into NSData object
NSData *data = [NSData dataWithBytes:&aStruct length:sizeof(myStruct)];

// retrieve data
myStruct *anotherStruct = (myStruct *)[data bytes];

// convert char array into NSString and only keep part required (without some weird random         characters)
NSString *receivedString = [[NSString alloc] initWithBytes:anotherStruct->stringToSend  length:anotherStruct->stringToSendLength encoding:NSASCIIStringEncoding];


A little old, but I'll throw in my 2 cents.

When I changed from char * to char string[30] i had to increase the length when I initialised my NSData. In addition, direct assignment of a char array is not possible. You either have to iterate the string and insert the characters one by one, or use strcpy like I did below.

This is how it finally looked like for me:

- (void)sendRandomWord:(NSString*)randomWord{
    MessageRandomWord message;
    message.message.messageType = kMessageTypeRandomWord;

    strcpy( message.randomWord, randomWord.UTF8String );

    NSData *data = [NSData dataWithBytes:&message length:sizeof(kMessageTypeRandomWord) + strlen(message.randomWord)*sizeof(char)];
    [self sendData:data];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜