Problem with NSStream SSL Connection

放肆的年华 提交于 2019-12-18 06:23:12

问题


I am using NSStream with SSL property for socket connection. It works fine when I use the local IP address such as 192.168.1.77. But if I use any server like www.xyz.com (it has SecurityLevelTLSv1), it shows an error error code:-9843, Message:Operation could not be completed. (NSUnknownErrorDomain error -9843.)

Here is my code:

-(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];
}

I tried using both NSStream and CFStream. I am getting the same error in both cases.

NSStreamEventOpenCompleted and NSStreamEventErrorOccurred events are called.

Please help me in this.

Thanks in advance.

Ramesh.P


回答1:


I solved this issue. The following line of code did everything.

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

Thanks, Ramesh.P



来源:https://stackoverflow.com/questions/1700819/problem-with-nsstream-ssl-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!