我有一些代码可以为启用了 SSL / TLS 的远程服务器创建一对CFStream
个对象。此代码在OS X上运行良好,但在iOS下运行时,它会失败。这是控制台日志:
2011-04-26 22:39:35.820 RemoteSample[92127:40b] connecting to 192.168.1.187:8099
2011-04-26 22:39:35.825 RemoteSample[92127:40b] INPUT: NSStreamEventOpenCompleted
2011-04-26 22:39:35.825 RemoteSample[92127:40b] OUTPUT: NSStreamEventOpenCompleted
2011-04-26 22:39:35.827 RemoteSample[92127:40b] INPUT: NSStreamEventErrorOccurred
2011-04-26 22:39:35.828 RemoteSample[92127:40b] Error on input stream: The operation couldn’t be completed. (OSStatus error -108.)
2011-04-26 22:39:35.829 RemoteSample[92127:40b] OUTPUT: NSStreamEventErrorOccurred
2011-04-26 22:39:35.829 RemoteSample[92127:40b] Error on output stream: The operation couldn’t be completed. (OSStatus error -108.)
OSStatus错误-108似乎是memFullErr,这简直太奇怪了,我不太清楚如何处理它。这在模拟器和设备上都会发生。它在Mac OS X应用程序中编译并运行时没有此问题。仅当为iOS构建时才会发生此错误。
花了几个小时尝试各种想法和大量搜索,我可以使用一些关于后续步骤的建议。
以下是代码:
CFReadStreamRef inCfStream = NULL;
CFWriteStreamRef outCfStream = NULL;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)host, port, &inCfStream, &outCfStream);
if (inCfStream && outCfStream)
{
CFReadStreamSetProperty(inCfStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
inStream = NSMakeCollectable(inCfStream);
outStream = NSMakeCollectable(outCfStream);
[inStream setDelegate:self];
[outStream setDelegate:self];
[inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
[NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
kCFNull,kCFStreamSSLPeerName,
nil];
if (CFReadStreamSetProperty(inCfStream, kCFStreamPropertySSLSettings, (CFTypeRef)properties) == FALSE)
{
NSLog(@"Failed to set SSL properties on read stream.");
}
inputBuffer = [[NSMutableData alloc] init];
[inStream open];
[outStream open];
}