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

  • SEARCH
  • Home
  • 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 7563559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:41:45+00:00 2026-05-30T13:41:45+00:00

Hi I am trying to create a packet that I want to send over

  • 0

Hi I am trying to create a packet that I want to send over the network to a server, I have pretty much got the packet sorted however its missing a length identifier which I need to calculate at the end of my method and add into the packet.

The packet structure is like this

  • leading value identifier (UInt16)
  • content size (UInt32)
  • packet Content (string)

currently my method looks something like this

- (NSMutableData *) addRegCode
{
    //Supply some default string for testing
    NSString *regCode = [[NSString alloc] initWithString:@"abcd1"];
    //create NSData object
    NSData *registrationCodeData = [regCode dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableData * RegistrationCodeMutableData = [[NSMutableData alloc] init]; //send this object
    //create leading value
    UInt16 leadingValue = 8;
    NSData * leadingValueData = [[NSData alloc] initWithBytes:&leadingValue length:sizeof(leadingValue)];

    //append data to mutableData
    [RegistrationCodeMutableData appendData:leadingValueData];
    [RegistrationCodeMutableData appendData:registrationCodeData];

    return RegistrationCodeMutableData;
}

what I would like to know is how to calculate the size of registrationCodeData and then add it between where I append leadingValueData & registrationCodeData

I think I have to be using dataWithBytes:length: but I’m not 100% sure of how to use this

  • 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-05-30T13:41:47+00:00Added an answer on May 30, 2026 at 1:41 pm

    this should do it.

    NSUInteger size = [registrationCodeData length];
    NSData *sizeData = [NSData dataWithBytes:&size length:sizeof(size)];
    [RegistrationCodeMutableData appendData:leadingValueData];
    [RegistrationCodeMutableData appendData:sizeData];
    [RegistrationCodeMutableData appendData:registrationCodeData];
    

    also of note using capital letters to start variable names is a little atypical and can be confusing to read!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty much to C# and I'm trying to create a simple network analyzer.
I am trying to send the following data to a server, that will be
I am trying to create a network diagnostics page that is exactly like the
I'm trying to create a program that will be able create and send tcp
Im trying to create a Speex Voip client and server. I have the basics
I'm trying to send an IP packet using c#. destAddress = IPAddress.Parse(192.168.0.198), destPort =
I am looking to create a client/server application that I can use to slit
I'm trying write a C program that sends an UDP packet to a given
I have a default Glassfish 3 installation and I am trying to create a
I am trying to create a data packet, using memcpy . I expect to

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.