Re: Reading NSInputStream
Re: Reading NSInputStream
- Subject: Re: Reading NSInputStream
- From: Jens Alfke <email@hidden>
- Date: Wed, 21 Oct 2009 21:18:43 -0700
On Oct 21, 2009, at 4:48 PM, Ashley Perrien wrote:
NSMutableData *returnMessage = [NSMutableData dataWithLength: 300];
[readStream read: [returnMessage mutableBytes] maxLength: 300];
NSMutableString *readData = [[[NSMutableString alloc]
initWithBytes: [returnMessage bytes] length: 300 encoding:
NSUTF8StringEncoding] autorelease];
NSLog(@"Read: %@", readData);
return readData;
This will always work as expected:
Read: 220 mx.google.com ESMTP...
IIRC, the read: method will block until there is data available, even
if the stream's scheduled with a runloop. So this call is working for
you, but at the expense of blocking your thread indefinitely until the
server responds. You should wait for the delegate calls instead to
notify you that data is available.
But only if I don't read from the stream twice. If all of the above
code is run, it fails with "Program received signal:
“EXC_BAD_ACCESS”." If I comment out the int len = [readStream...
section, the NSMutableData section reads fine and gets data.
Well, from your code it looks like you get the buffer pointer from
getBuffer: and then try to reuse that pointer when calling read:.
That's a bad idea — you're telling the stream to copy data into its
own internal buffer, which is likely to cause confusion or worse. You
should treat the buffer returned from getBuffer: as read-only.
Have you read the "Stream Programming Guide For Cocoa"?
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden