2009-11-09 65 views
5

我正在使用帶有SSL屬性的NSStream進行套接字連接。當我使用本地IP地址如192.168.1.77時,它工作正常。但是如果我使用任何服務器(如www.xyz.com)(它具有SecurityLevelTLSv1),則顯示錯誤 錯誤代碼:-9843,消息:操作無法完成。 (NSUnknownErrorDomain錯誤-9843)NSStream SSL連接的問題

這裏是我的代碼:

-(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事件被調用。

請幫我這個。

在此先感謝。

Ramesh.P

回答

6

我解決了這個問題。 以下代碼行完成了一切。

[settings setObject:@"www.xyz.com" forKey:(NSString *)kCFStreamSSLPeerName]; 

感謝, Ramesh.P