2
votes

I am developing multiplayer game using game centre, Overall Code is working well if my both device is 64bit or 32bit, but if my one device is 32bit and other is 64bit than sending data is not correct, please help me,

thanks in Advance

- (void)sendDataToPlayers:(void *)data length:(NSInteger)length

{ NSError *error = nil;

NSLog(@"lenth %i",(int)data);
NSData *package = [NSData dataWithBytes:data length:length];



[self.currentMatch sendDataToAllPlayers:package withDataMode:GKMatchSendDataReliable error:&error];

[self setLastError:error];

} - (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {

[self.transportDataDelegate onReceivedData:data fromPlayer:playerID];

}

2
I guess it depends what's in the data. If you use types with different sizes for different platforms, then that will cause a problem. At least you don't have to worry about endian; which is just as well. - trojanfoe
different sizes for different platform like what??? few msg are receiving correct like strings bit not integer or user define types??? how can i fix this - Syed Muhammad Hassan
long for example (or anything typedefd to long). - trojanfoe
yes i am using typedef like ` typedef enum { kPacketTypeScore = 1, kPacketTypePosition, kPacketTypeRandomNumber, kPacketTypeReRandomNumber, kPacketTypeMyFigure, kPacketTypeFigureResponse, kPacketTypeGameBegin, kPacketTypeGameEnd, kPacketTypeRegion } EPacketTypes; typedef struct { EPacketTypes type; } SBasePacket;` - Syed Muhammad Hassan
That's not a problem, however you need to ensure you only ever use types like int8_t, uint32_t, etc. for the data you exchange, and then you will be OK. - trojanfoe

2 Answers

2
votes

I Solved this problem, by changing the formate of data packet, i was sending user define struct which has different data types like integers and char when this send to the other player and decode from NSdata its receives nothing like zeros and null,

i change my packet formate like now i am sending NSString,

 NSString *moveString =[ NSString  stringWithFormat:@"/,%i,%i,%i,%i",oMoveMade.getFrom(),oMoveMade.getTo(),oMoveMade.getFromsquare(),oMoveMade.getTosquare()];
    NSString *temp = [ NSString stringWithFormat:@"%i,", oMoveMade.getFrom()];
    [moveString stringByAppendingString:temp];
    [moveString stringByAppendingString:[ NSString stringWithFormat:@"%i,",oMoveMade.getTo()]];
    [moveString stringByAppendingString:[ NSString stringWithFormat:@"%i,", oMoveMade.getFromsquare()]];
    [moveString stringByAppendingString:[ NSString stringWithFormat:@"%i", oMoveMade.getTosquare()]];

    NSLog(@"movestring %@",moveString);


 NSData *package = [moveString dataUsingEncoding:NSUTF8StringEncoding];

   [self.currentMatch sendDataToAllPlayers:package      withDataMode:GKMatchSendDataReliable  error:&error];

this is not exactly solution but in my case its solved my problem, if some one has better solution most welcome to help us

2
votes

You could for example look at NSJSONSerialization, which allows you to convert any dictionary or array containing dictionaries, arrays, numbers, strings, and NSNull values to NSData in a 100% portable way. There is one method turning dictionary or array into NSData, and another to turn NSData back into the exact same dictionary or array.