0

我(显然)设法建立了一个 ftp 连接,但无法从中读取任何内容,并且有充分的理由:在连接超时之前我无法读取。

这是我的代码:

标题:

NSInputStream *iStream;
NSOutputStream *oStream;

执行:

        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

对于事件处理,我使用它,基于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;
    }
}

我从这篇文章中省略了 oStream 设置以保持最小化。

我还没有向 FTP 发送切换到 ssl 的请求。

非常感谢任何帮助,因为我发现 Xcode 对于调试来说非常糟糕(在失败的步骤上没有异常或错误消息)。

  • 查克
4

1 回答 1

0

请参阅轮询与运行循环调度。注意关键词vs

此外,我认为责怪 Xcode 的调试功能是不公平的。Xcode 是 IDE,非常适合调试。objc_exception_throw()当出现问题并且我在调试会话期间设置断点或-[NSException raise]捕获异常时,API 应该引发异常(或返回 nil) 。

要考虑的另一点:您没有任何保护代码。这意味着您忽略了连接可能无法成功建立的可能性。错误检测的一部分是确保您自己的代码检测并处理故障,尤其是在涉及网络通信的情况下。

于 2010-04-06T14:01:30.107 回答