我(显然)设法建立了一个 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 对于调试来说非常糟糕(在失败的步骤上没有异常或错误消息)。
- 查克