I searched the web for a long time...I didn't find an answer for my issue, so I decided to post here. I try to establish a connection to a NNTP-server using NSStream.
In a test-program, I open the streams and send a message. The delegate-method (stream:handleEvent:
) is called twice for the output-stream (NSStreamEventOpenCompleted
, NSStreamEventHasSpaceAvailable
) but never for the input-stream!
Why does the input stream never call the delegate? Any ideas?
Basically, the code looks like this:
init and open streams:
CFReadStreamRef tmpiStream;
CFWriteStreamRef tmpoStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)SERVER, PORT, &tmpiStream, &tmpoStream);
iStream = (__bridge NSInputStream *) tmpiStream;
oStream = (__bridge NSOutputStream *)tmpoStream;
[iStream setDelegate:self];
[oStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
send message:
NSData *data = [[NSData alloc] initWithData:[messageString dataUsingEncoding:NSASCIIStringEncoding]];
[oStream write:[data bytes] maxLength:[data length]];
receive messages:
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
NSLog(@"EventCode: %i", eventCode);
//switch-case-statement...(using constants - NSStreamEventOpenCompleted...)
}
The class which contains that code inherits from NSObjects and implements NSStreamDelegate
.
(iOS5 with ARC)
Thx for any help!
EDIT: I just tried "polling" after opening streams like this - it's working:
while (![iStream hasBytesAvailable])
{}
uint8_t buffer[1024];
int len;
NSString *str = @"";
while ([iStream hasBytesAvailable])
{
len = [iStream read:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSISOLatin1StringEncoding];
if (output != nil)
{
str = [str stringByAppendingString:output];
}
}
}
NSLog(@"Response: %@", str);
But, for sure, I still need a better (async) solution ;)