Re: Cocoa-dev Digest, Vol 6, Issue 1522
Re: Cocoa-dev Digest, Vol 6, Issue 1522
- Subject: Re: Cocoa-dev Digest, Vol 6, Issue 1522
- From: Ashley Perrien <email@hidden>
- Date: Wed, 21 Oct 2009 21:30:08 -0500
Really can't tell, because you're only posting isolated code
fragments, instead of complete and self-contained fragments that
show the entire context.
So here's the unadulterated method I'm trying to get working (not very
useful at this point but if I can get the reading right....):
-(void) openConnection {
// next 4 lines are actually set up elsewhere
NSHost *mailHost = [NSHost hostWithName: @"mail.me.com"];
int mailPortNumber = 143;
NSInputStream *readStream;
NSOutputStream *writeStream;
[NSStream getStreamsToHost: mailHost port: mailPortNumber
inputStream: &readStream outputStream: &writeStream];
[readStream retain];
[readStream setDelegate: self];
[readStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:
NSDefaultRunLoopMode];
[readStream open];
[writeStream retain];
[writeStream setDelegate: self];
[writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:
NSDefaultRunLoopMode];
[writeStream open];
BOOL youWantThisToWork = YES;
if (youWantThisToWork) {
NSMutableData *returnMessage = [NSMutableData dataWithLength: 300];
int bytesRead = [readStream read: [returnMessage mutableBytes]
maxLength: 300];
NSMutableString *readData = [[[NSMutableString alloc] initWithBytes:
[returnMessage bytes] length: 300 encoding: NSUTF8StringEncoding]
autorelease];
NSLog(@"Read: %d bytes\n%@", bytesRead, readData);
}
// Output: Read: 300 bytes \n * OK [CAPABILITY mmp0841 IMAP4.....
else {
if ([readStream hasBytesAvailable])
NSLog(@"Has Bytes");
else
NSLog(@"No Bytes");
uint8_t *readBuffer;
NSUInteger bufferLength;
BOOL gotBuffer = [readStream getBuffer: &readBuffer length:
&bufferLength];
if (gotBuffer) {
int len = [readStream read: readBuffer maxLength: bufferLength];
if (len <= 0)
NSLog(@"nothing read");
else
NSLog(@"Read: %@", [[[NSString alloc] initWithBytes: readBuffer
length: bufferLength encoding:NSUTF8StringEncoding] autorelease]);
}
else {
NSLog(@"Did not get a buffer");
}
}
// Output: No Bytes , Did not get a buffer
// This second section is what I'd like to get working So I can
actually have some error checking in the code rather than just reading
and hoping something comes in and it doesn't hang.
}
[readStream read: [returnMessage mutableBytes] maxLength: 300];
You're neglecting the return value, which indicates the actual
number of bytes read. You're probably lucking out that the
mutableBytes have been zeroed, so any data less than 300 bytes ends
up with a terminating nul character.
I'm not entirely sure why you mention the null character. Even if I
change the above code to only read 10 bytes the output is:
Read: 10 bytes
* OK [CAPA
So even if all 10 bytes are actually data, it reads it fine. My
question is, when there obviously is data there to be read
hasBytesAvailable returns no and so does getBuffer. But if I just grab
the stream and push it into a data object, it works fine until there
actually isn't data available and then it just hangs.
I really appreciate any help with this. I'm fairly comfortable with
cocoa but VERY new to trying the networking side of it. I assume I'm
missing something small, I just can't figure out what it is.
Ashley
_______________________________________________
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