1

I am trying to get simple HTTP communications working using CFStream, however with the below code I still get a connection success even when I give an invalid URL (wwwww.yahoo.com).

Can someone point to me what is wrong with this code? I am expecting the call to CFReadStreamOpen() to fail but it is succeeding even though the URL is bad.

I haven't been able to find a simple CFNetwork example which does HTTP (without use of NSURL) that works on ARC. If anyone knows of such sample code please let me know.

- (void) test
{

    CFStringRef m_host = CFStringCreateWithCString(NULL, "wwwww.yahoo.com", kCFStringEncodingASCII);

    int m_port = 80;
    CFHostRef host;

    NSLog(@"Attempting to (re)connect to %@:%d", m_host, m_port);

    {

        CFReadStreamRef     m_in = NULL;
        CFWriteStreamRef    m_out = NULL;

        host = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)m_host);
        if (!host)
        {
            NSLog(@"Error resolving host %@", m_host);
        }

        CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, host , m_port, &m_in, &m_out);
        CFRelease(host);

        CFStreamClientContext context = {0, nil,nil,nil,nil}; 


        if (CFReadStreamSetClient(m_in, kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, networkReadEvent, &context))
        {
            CFReadStreamScheduleWithRunLoop(m_in, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
        }

        if (CFWriteStreamSetClient(m_out, kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, networkWriteEvent, &context))
        {
            CFWriteStreamScheduleWithRunLoop(m_out, CFRunLoopGe   tCurrent(),kCFRunLoopCommonModes);
        }        

        BOOL success = CFReadStreamOpen(m_in);
        CFErrorRef error = CFReadStreamCopyError(m_in);
        if (!success || (error && CFErrorGetCode(error) != 0))
        {
            NSLog(@"Connect error %s : %ld", CFErrorGetDomain(error), CFErrorGetCode(error));
            [NSThread sleepForTimeInterval:5.0];
        }

        success = CFWriteStreamOpen(m_out);
        error = CFReadStreamCopyError(m_in);
        if (!success || (error && CFErrorGetCode(error) != 0))
        {
            NSLog(@"Connect error %s : %d", CFErrorGetDomain(error), CFErrorGetCode(error));
            [NSThread sleepForTimeInterval:5.0];
        }
        else 
        {
            NSLog(@"Connected");
        }

    }
}

1 Answer 1

0

For asynchronous code such as yours, you need to set up the delegate and catch the error there. Check out http://iphonedevsdk.com/forum/iphone-sdk-development/17542-cfstream-catch-error-in-connection.html

According the the Apple Developer site, "If the stream can open in the background without blocking, this function always returns true.". Meaning the request to open the stream is sent out on a different thread and hence the function returns true when you call it and starts the connection process on the background thread. All read/write calls on the respective streams then become blocking until the background thread finishes opening up the stream. You will be able to see this if you set up a break point on "CFReadStreamOpen(m_in);" and see what happens after that single line executes. Hope this answers your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.