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 6070741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:58:04+00:00 2026-05-23T09:58:04+00:00

So I’m making an iOS app that constantly sends and array of points through

  • 0

So I’m making an iOS app that constantly sends and array of points through NSStreams, but because sometimes the sender writes two arrays before the receiver receives one, I decided to first write the length, then the array data itself, so that the receiver knows how many bytes to process.

sender:

if (_outStream && [_outStream hasSpaceAvailable]){
    const uint8_t *buffer = [data bytes];
    uint64_t length = CFSwapInt64HostToLittle([data length]);
    NSLog(@"Sending bytes with length: %llu", length);
    int er1 = [_outStream write:(const uint8_t *)&length maxLength:sizeof(length)];
    int er2 = [_outStream write:buffer maxLength:length];  
    if (er1 < 0 || er2 < 0) {
        [self _showAlert:@"Failed sending data to peer"];
    }
}

receiver:

case NSStreamEventHasBytesAvailable:
    {
        if (stream == _inStream) {
            uint8_t *b;
            int len = 0;
            len = [_inStream read:b maxLength:8];
            uint64_t dataLength = CFSwapInt64LittleToHost(*b);
            NSLog(@"Received bytes with length: %llu", dataLength);
            if(len < 0) {
                if ([stream streamStatus] != NSStreamStatusAtEnd)
                    [self _showAlert:@"Failed reading data from peer"];
            } else if (len > 0){
                uint8_t bytes[dataLength];
                int length = [_inStream read:bytes maxLength:dataLength];

                [currentDownload appendBytes:bytes length:length]; 
                 id pointsArray = [NSKeyedUnarchiver unarchiveObjectWithData:currentDownload];
                [currentDownload release];
                currentDownload = [[NSMutableData alloc] init];
                if ([pointsArray isKindOfClass:[NSArray class]]) {
                    [drawScene addNewPoint:[[pointsArray objectAtIndex:0] CGPointValue] previousPoint:[[pointsArray objectAtIndex:1] CGPointValue]];
                }

            }
        }
        break;
    }

The problem is that the receiver receives an incorrect integer, hence it reads an incorrect amount of bytes.

Can anyone help me with 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-23T09:58:05+00:00Added an answer on May 23, 2026 at 9:58 am

    Could it be that when you read the data, you’re starting with the introductory bits intended to convey length, rather than starting with the actual data?

    For:

    [currentDownload appendBytes:bytes length:length];
    

    try substituting something like this:

    NSRange rangeLeftover = NSMakeRange(sizeof(uint8_t), [currentDownload length] - sizeof(uint8_t));
    NSData *dataLeftover = [currentDownload subdataWithRange:rangeLeftover];
    uint8_t bytesLeftover[[dataLeftover length]];
    [dataLeftover getBytes:&bytesLeftover length:[dataLeftover length]];
    currentDownload = [NSMutableData data]; // clear
    [currentDownload appendBytes:bytesLeftover length:[dataLeftover length]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

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.