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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:47:56+00:00 2026-05-27T20:47:56+00:00

I try to read and write data from/to Socket with NSStream. Here is my

  • 0

I try to read and write data from/to Socket with NSStream. Here is my code for connect :

- (void)connect
{
  [NSStream getStreamsToHostNamed:APIC_HOST_ADDR 
                             port:APIC_HOST_PORT
                      inputStream:&inStream
                     outputStream:&outStream];
  [inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  [outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

  inStream.delegate = self;
  outStream.delegate = self;

  if ([inStream streamStatus] == NSStreamStatusNotOpen)
    [inStream open];

  if ([outStream streamStatus] == NSStreamStatusNotOpen)
    [outStream open];

}

and for input stream i implement the delegate methods to recieve events

- (void)handleInputStreamEvent:(NSStreamEvent)eventCode
{
  switch (eventCode) {
    case NSStreamEventHasBytesAvailable:
    {
    int bytesRead;
    if (data == nil) {
      data = [[NSMutableData alloc] init];
    }
    uint8_t buf[1024];
    unsigned int len = 0;
    len = [inStream read:buf maxLength:1024];
    if(len>0) {
      @try {
        [data appendBytes:(const void *)buf length:len];
      }
      @catch (NSException *exception) {
        NSLog(@"Fail: %@", exception);
      }
      @finally {
        NSLog(@"Finally");
        bytesRead += len;
      }
    } else {
      NSLog(@"No Buffer");
    }  

    NSString *str = [[NSString alloc] initWithData:data 
                                          encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);
    [str release];
    [data release];        
    data = nil;
    } break;
    case NSStreamEventErrorOccurred:
    {
      NSError *theError = [inStream streamError];
      NSLog(@"Error reading stream! ,Error %i: %@",[theError code], [theError localizedDescription]);
      [self disconnect];
      [self connect];
    } break;
  }
}

[NSStream read:maxLength:] always returns maximum unsigned integer value. Eventually i get this error:

Fail: *** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (4294967295)

Why does read mehod return this big value? Does it really read that much bytes? (I don’t think so) 🙂

PS: Socket Stream server is ok. it reads and writes data to other clients also and there is no problem.

  • 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-27T20:47:57+00:00Added an answer on May 27, 2026 at 8:47 pm

    I resolved the problem. I was writing data without observing if has space available in output stream.

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

Sidebar

Related Questions

In the following code, I try to read data from a plist: -(void)readPreferences {
I try to read data from one PNG file, and want to write this
I want to read some data from a device connected to a COM port.
What I am trying to do is to read in data from a socket
I try to read file /proc/'pid'/status, using c program. The code is as follows,
I'm using the following code to try to read the results of a df
I get a warning in MSVC++ when I try to read an integer from
I am having issues reading data from a socket. Supposedly, there is a server
My iPhone app will have read-only system data AND read/write user data (stored either
Server side code: static void Main(string[] args) { static Socket sck; Console.WriteLine(Server Started...); sck

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.