I’ve been developing a multiplayer game based on RayWenderlich’s tutorial, my problem is that i cannot send any NSString over NSData, i’ve tried many ways of encoding and decoding (UTF8) etc, but i always get something similar to \204(upside down ?)B(upside down ?) when i do a printfon the receiving side, but when i do an NSLog says (null).
here’s the modified parts of the .h
typedef struct {
Message message;
const char * theTeam;
} MessageGameBegin;
here’s the .m code that’s driving me nuts:
(void)sendGameBegin:(NSString *) team {
NSString *strSQL = [[NSString alloc]init];
strSQL = team;
const char *bar = [strSQL cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"HOOOOOE %@\n",[[NSString alloc] initWithCString:bar encoding:NSUTF8StringEncoding]);
MessageGameBegin message;
message.theTeam = bar;
message.message.messageType = kMessageTypeGameBegin;
NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageGameBegin)];
[self sendData:data];
}
then in the receive,
....else if (message->messageType == kMessageTypeGameBegin) {
MessageGameBegin * beginMessage = (MessageGameBegin *) [data bytes];
myDecoded = [[NSString alloc] initWithUTF8String:beginMessage->theTeam];
NSLog(@"NSLOOOOOOOG %@\n",myDecoded);
printf(" BEGING MESS %s\n",beginMessage->theTeam);
CCLOG(@"cl BEGING MESS %@\n",myDecoded);
[self setGameState:kGameStateActive];
self.enemy = [CCSprite spriteWithFile:myDecoded];
}
the NSLog(@"HOOOOOE %@\n.. doesn’t even print into the console even though i’m 100% its executed
NSLog(@"NSLOOOOOOOG %@\n".. returns (null)
printf gives weird humanly unreadable outputs like i showed before
the CCSprite line cant find the file because it says its nil
I need to know what im doing wrong in this attempt to send NSSTrings, or even a better way to do it.. please i’d appreciate your help, i’ve been ripping my hair out for 2 days since i’m new to objective c
heres the tutorial i’m going off on, it has a source you can download at the bottom of the part2
http://www.raywenderlich.com/3276/how-to-make-a-simple-multiplayer-game-with-game-center-tutorial-part-12
Edit:
bottom line the whole goal is to be able to send and receive a string in data… and i gfailed miserably so help would a appreciated
@borrrden this is what i got from your help
sending:
MessageGameBegin message;
message.theTeam = bar;
message.message.messageType = kMessageTypeGameBegin;
//NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageGameBegin)];
NSMutableData *data = [NSMutableData new];
[data appendBytes:&message length:sizeof(MessageGameBegin)];
[data appendBytes:message.theTeam length:strlen(message.theTeam)];
data.length = sizeof(MessageGameBegin) + strlen(message.theTeam);
[self sendData:data];
receiving:
MessageGameBegin msg;
msg.theTeam = [[data subdataWithRange:NSMakeRange(sizeof(msg.message.messageType), data.length-sizeof(msg.message.messageType))] bytes];
NSLog(@"NSLOOOOOOOG %@\n",[[NSString alloc] initWithCString:msg.theTeam encoding:NSUTF8StringEncoding]);
returns something that looks better (upside down ?)ІOarizona.png it seems like i got the data length wrong or something.. i want it to return arizona.png , it is constantly 3 characters off.. the 3 infront of arizona, so in this case would it be recomended to just trim off the 3 or is there another way?
sizeofis not a magic function. It doesn’t automatically know how long your struct is in the heap. It only knows how many bytes it takes up on the stack, which is however longMessageis, plus 4 bytes for a char pointer. So you will end up getting a pointer to where the data was on your original device, which when received on a new device will just be garbage (you are lucky it doesn’t crash). You need to encode the string directly into theNSDataobject, or just take the easier route and make a proper class. I recommend making a class if you are going to be storing pointers. Then you can just implementNSCodingto easily convert to and fromNSDataEDIT:
If you really must do the struct yourself, you are going to have to manually put together
NSData. I still recommend simply making a class.Let’s say I have this struct:
I can encode it like this:
This makes some severe assumptions about the string though such as endianness (which won’t matter if you are sending between two iOS devices, since they will both be the same) and that the string is 1-byte per character. If your string is UTF-16 for some reason, this will fail miserably, since it will potentially reach a NULL termination character early. Can you see now why making a class will make your life much easier?
NSStringwill handle all of this nonsense for you.If that is OK, then you can continue on the other side like this: