Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:20:13+00:00 2026-06-18T06:20:13+00:00

I’ve been developing a multiplayer game based on RayWenderlich’s tutorial, my problem is that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:20:14+00:00Added an answer on June 18, 2026 at 6:20 am

    sizeof is 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 long Message is, 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 the NSData object, 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 implement NSCoding to easily convert to and from NSData

    EDIT:

    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:

    typedef struct
    {
        int testInt;
        const char *testString;
    } SO2Test;
    

    I can encode it like this:

    SO2Test test;
    test.testInt = 5;
    test.testString = "TEST";
    NSMutableData *data = [NSMutableData new];
    [data appendBytes:&(test.testInt) length:sizeof(int)];
    [data appendBytes:test.testString length:strlen(test.testString)];
    data.length = sizeof(int) + strlen(test.testString);
    

    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? NSString will handle all of this nonsense for you.

    If that is OK, then you can continue on the other side like this:

    SO2Test test2;
    int *intPtr = (int *)[[data subdataWithRange:NSMakeRange(0, sizeof(int))] bytes];
    test2.testInt = *intPtr;
    test2.testString = [[data subdataWithRange:NSMakeRange(sizeof(int), data.length-sizeof(int))] bytes];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.