我正在使用带有 SSL 属性的 NSStream 进行套接字连接。当我使用 192.168.1.77 等本地 IP 地址时,它工作正常。但是如果我使用任何像 www.xyz.com 这样的服务器(它具有 SecurityLevelTLSv1),它会显示错误错误代码:-9843,消息:无法完成操作。(NSUnknownErrorDomain 错误 -9843。)
这是我的代码:
-(void) startSocket{
NSURL *website = [NSURL URLWithString:@"www.xyz.com"];
NSHost *host = [NSHost hostWithName:[website host]];
if(host) {
NSLog(@"Valid host");
[NSStream getStreamsToHost:host port:443 inputStream:&iStream outputStream:&oStream] ;
[self openStream];
}.
-(void)openStream{
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:1];
[settings setObject:(NSString *)NSStreamSocketSecurityLevelTLSv1 forKey:(NSString *)kCFStreamSSLLevel];
[settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
[iStream retain];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
CFReadStreamSetProperty((CFReadStreamRef)iStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
//[iStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];
[iStream open];
[oStream retain];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
CFWriteStreamSetProperty((CFWriteStreamRef)oStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
//[oStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];
[oStream open];
}
我尝试同时使用 NSStream 和 CFStream。在这两种情况下,我都会遇到相同的错误。
NSStreamEventOpenCompleted 和 NSStreamEventErrorOccurred 事件被调用。
请帮助我。
提前致谢。
拉梅什