I am new to Cocoa/iOS – iphone platform
I am trying to work with network streams using Core Foundations. I am following the CFNetworking guide but I can’t get a simple test to work. The following compiles and runs but nothing is recorded to NSLog.
I am providing the Callback, clientCB, to CFReadStreamSetClient and scheduling the readStream on the run loop (at least that’s the idea).
I was expecting the CallBack to report something.
Can someone tell me where I am going wrong? I have telnet’d to this server and it works. Any suggestions would be helpful.
Thanks.
void clientCB(CFReadStreamRef stream, CFStreamEventType event, void *myPtr)
{
switch(event) {
case kCFStreamEventHasBytesAvailable:{
UInt8 buf[BUFSIZE];
CFIndex bytesRead = CFReadStreamRead(stream, buf, BUFSIZE);
if (bytesRead > 0) {
NSLog(@"Server has data to read!");
}
break;
}
case kCFStreamEventErrorOccurred:
NSLog(@"A Read Stream Error Has Occurred!");
case kCFStreamEventEndEncountered:
NSLog(@"A Read Stream Event End!");
default:
break;
}
}
- (IBAction) connectToServer: (id) sender
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)@"irc.freenode.net");
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, host, 6667, &readStream, &writeStream);
CFStreamClientContext myContext = {
0,
self,
(void *(*)(void *info))CFRetain,
(void (*)(void *info))CFRelease,
(CFStringRef (*)(void *info))CFCopyDescription
};
CFOptionFlags registeredEvents = kCFStreamEventHasBytesAvailable |
kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered;
if(CFReadStreamSetClient(readStream, registeredEvents, clientCB, &myContext))
{
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
}
}
It seems that you forgot to call CFReadStreamOpen.