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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:16:46+00:00 2026-05-31T10:16:46+00:00

I have a question concerning my last post But I think at first I

  • 0

I have a question concerning my last post

But I think at first I should provide some code:

This is the function to handle my created object

- (void)handleObject:(NSMutableData *)object {

NSLog(@"[object length] = %d", [object length]);
Byte *byteArray;
byteArray = (Byte *)[object bytes];

switch (byteArray[5]) {

    case 0: NSLog(@"Login OK"); 
        break;

    case 1: NSLog(@"Exit. Server disconnect");
        break;

    case 2: NSLog(@"Ping erhalten");
        break;

    case 4: NSLog(@"Points received");
        break;

    case 6: NSLog(@"transfer of POLYLINE vector objects");
        break;

    case 8: NSLog(@"transfer of POLYGON vector objects");
        break;

    case 9: NSLog(@"transfer of Render Rule");
        break;

    case 10: {

        NSLog(@"end of response for RequestVectorObjects");                                 
        isLoading = FALSE;
    }
        break;

    case 11: NSLog(@"Background Colorcode: %d", (byteArray[6] & 0xFF));
        break;    

    default: NSLog(@"From server: default value");
        break;
}

}

And I here I receive the data from a stream

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

switch(eventCode) {

    case NSStreamEventHasBytesAvailable:
    {
        isLoading = YES; 

        while (isLoading) {               

            uint8_t bufSize[4];            

            int packetLength = 0; 

            [(NSInputStream *)stream read:bufSize maxLength:4];
            packetLength = [self bytesToInt:bufSize withOffset:0];
            NSLog(@"packetLength: %d", packetLength);

            [tempStore appendBytes:bufSize length:4];

            if (packetLength == 25600) {

                loggedIn = YES;
                uint8_t buf[4];
                [(NSInputStream *)stream read:buf maxLength:4];
                [tempStore appendBytes:buf length:4];
                [self handleObject:tempStore];
            }
            else {

                uint8_t buf[packetLength];
                [(NSInputStream *)stream read:buf maxLength:packetLength];
                [tempStore appendBytes:buf length:packetLength];
                [self handleObject:tempStore];                    
            }
            [tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
            [tempStore setLength:0];
        }           
        NSLog(@"Finished loading objects");                 

    } break;      

}

It all works just fine with the simulator or if I debug the application on the iPhone, but if try to run it on the device I always get BAD_EXCESS, because the value of packetLength is wrong and so I try to read too much byte into my buffer.

Now to my question: How can it be that all values are right in debug mode, but get totally messed up in run mode? Is there anything I could to avoid that problem or I should be aware of?

Your help is much appreciated

Here is my BytesToInt Function, but I think it works as it should, if the received values are correct

- (int)bytesToInt:(Byte *)b withOffset:(int)offset {

int ret = 0;
for (int i = 0; i < 4; i++) {

    ret |= (b[offset + i] & 0xFF) << (3-i)*8;
}    
return ret;

}

  • 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-31T10:16:47+00:00Added an answer on May 31, 2026 at 10:16 am

    I finally did it by looping the received data until I reached my maxLength value. It wasn’t too hard to fix, but I wasn’t acting quite smart…

    Well, here is how I did it:

    isLoading = YES; 
    
            while (isLoading) {               
    
                uint8_t bufSize[4];            
    
                int packetLength , maxLength, len;
    
                maxLength = 4;
                len = [(NSInputStream *)stream read:bufSize maxLength:maxLength];
                NSLog(@"len = %d", len);
                packetLength = [self bytesToInt:bufSize withOffset:0];
                NSLog(@"packetLength: %d", packetLength);
    
                [tempStore appendBytes:bufSize length:maxLength];
    
                if (packetLength == 25600) {
    
                    loggedIn = YES;
                    maxLength = 4;
                    uint8_t buf[maxLength];                    
                    len = [(NSInputStream *)stream read:buf maxLength:maxLength];
                    NSLog(@"len = %d", len);
                    [tempStore appendBytes:buf length:maxLength];
                }
                else {
                    maxLength = packetLength;
                    BOOL allDataReceived = NO;
    
                    while (!allDataReceived) {
    
                        uint8_t buf[maxLength];                    
                        len = [(NSInputStream *)stream read:buf maxLength:maxLength];                                                                    
                        NSLog(@"len = %d", len);                       
    
                        if (len < maxLength) {
    
                            maxLength -= len;
                            [tempStore appendBytes:buf length:len];
                        }
                        else {
    
                            allDataReceived = YES;
                            [tempStore appendBytes:buf length:len];
                        }                            
                    }                    
                }
                [self handleObject:tempStore];
                [tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
                [tempStore setLength:0];
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question concerning this code which I want to run on QNX:
I have question concerning a function I created. I would like to show the
I have a question concerning using the toggle function. I have a template that
I have a question concerning holding common code in a base class and having
i have a small question concerning away3D. I have some cubes with the exact
I have a SQL efficiency question. This is concerning the Norwegian national lottery. They
In my last question I posted some sample code on how I was trying
I have a question concerning page speed and code optimization. I have a page
I have a question concerning functions with jQuery. I have a function that once
I have a question concerning Monotouch. The situation: I have 2 ViewControllers. The first

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.