Question

I (apparently) manage to make a ftp connection, but fail to read anything from it, and with good cause: I don't reach the reading until the connection has timed out.

Here's my code:

header:

NSInputStream *iStream;
NSOutputStream *oStream;

implementation:

        NSHost *host = [NSHost hostWithAddress:@"127.0.0.1"];
        [iStream open]; // SOLUTION changes
        [NSStream getStreamsToHost:host port:3333 inputStream:&iStream outputStream:&oStream];

        NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:1];
        [settings setObject:(NSString *)NSStreamSocketSecurityLevelTLSv1 forKey:(NSString *)kCFStreamSSLLevel];
        [settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
        [settings setObject:@"127.0.0.1" forKey:(NSString *)kCFStreamSSLPeerName];

        /*[iStream retain];   
        [iStream setDelegate:self];
        [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        CFReadStreamSetProperty((CFReadStreamRef)iStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
        [iStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];*/
        //[iStream open]; SOLUTION changes: moved up

For eventhandling I use this, based on http://www.cocoadev.com/index.pl?NSStream:

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
    if (aStream == iStream) {
        [self handleInputStreamEvent:eventCode];
    } else if (aStream == oStream) {
        [self handleOutputStreamEvent:eventCode];
    }
}
- (void)handleInputStreamEvent:(NSStreamEvent)eventCode
{
    switch (eventCode) {
    // SOLUTION changes: new inputstream handler
    case NSStreamEventHasBytesAvailable:
        if(!_data) {
            _data = [[NSMutableData data] retain];
        }
        uint8_t buf[1024];
        unsigned int len = 0;
        len = [(NSInputStream *)iStream read:buf maxLength:1024];
        if(len) {
            [_data appendBytes:(const void *)buf length:len];
            [bytesRead setIntValue:[bytesRead intValue]+len];
        }
        [self readBytes];
        break;
    case NSStreamEventOpenCompleted:
        NSLog(@"NSStreamEventOpenCompleted");
        break;
    case NSStreamEventEndEncountered:
        NSLog(@"NSStreamEventEndEncountered");
        break;
    case NSStreamEventErrorOccurred:
        NSLog(@"An error occurred on the input stream.");
        break;
    }
}

I've left out oStream setup from this post to keep it minimal.

I have not sent a request to the FTP to switch to ssl yet.

Any help is greatly appreciated as I find Xcode quite horrible for debugging (no exception or error msg on failed steps what so ever).

  • Chuck
Was it helpful?

Solution

See Polling Versus Run-Loop Scheduling. Note the key word versus.

Further, I don't think it's fair to blame Xcode's debugging capabilities. Xcode is the IDE and works quite well for debugging. The API is what should be raising exceptions (or returning nil) when something goes wrong and I've never had a problem setting a breakpoint on objc_exception_throw() or -[NSException raise] to catch exceptions during my debugging sessions.

Another point to consider: You have no guard code whatsoever. This means you're ignoring the possibility that a connection might not be established successfully. Part of error detection is making sure your own code detects and handles failures, especially where network communications are involved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top