views:

319

answers:

1

Hi,

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
A: 

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.

Joshua Nozzi
Thanks for the link.And valid points on my, what probably seemed like an, attack on xcode. However, my mix of polling is only when I read. No doubt it is a mistake, and I have removed it, I still don't ever get a response that the stream has bytes available. I can confirm that a connection IS made, as I can see server side that it connects, and the server sends a "220" (ftp) hello message.
James
- (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) { case NSStreamEventHasBytesAvailable: [self readBytes]; break; }}is what I've set up to handle the stream events
James
Best to update the original post with the relevant code. Code is not formatted in comments. You should definitely update the code listing in your original question for clarity.
Joshua Nozzi
Yes, sorry. One moment.
James
A few things stand out here. 1) Why mix CFReadStreamSetProperty() and the -setProperty:forKey: when setting up the stream? 2) Do you ever get a NSStreamEventOpenCompleted or NSStreamEventEndEncountered? 3) I'm not sure if it's valid to have a case after a default in your switch/case statement, but I might be wrong there. 4) You're setting properties on the stream after it's been scheduled - is that wise? 5) Have you tried not setting any properties?
Joshua Nozzi
1: Based on http://stackoverflow.com/questions/1700819/problem-with-nsstream-ssl-connection I did so to ensure it will allow selfsigned cetificates (when the time comes. However I have not told the server to switch to SSL yet).2: OpenCompleted yes.3: Fixed4-5: I'm trying to run it without any properties now. And I receive the correct welcome message now. Thanks. I'll update the code to what I got working now thanks to you.
James