gpt4 book ai didi

swift - TCP 套接字流和 SSL 与 Swift

转载 作者:行者123 更新时间:2023-12-02 11:03:20 25 4
gpt4 key购买 nike

我正在处理一个需要连接到套接字的简单客户端。此套接字需要 SSL...我正在尝试配置客户端以支持 SSL,但收到此错误:

CFNetwork SSLHandshake failed (-9807)

这是我编写的用于配置套接字的代码。你看到什么奇怪/错误的地方了吗?另外...服务器正在本地主机上运行,​​而我目前正在模拟器上运行iOS应用程序...这可能是一个问题吗?

class MySocket:NSObject {

var inputStream: InputStream!
var outputStream: OutputStream!

func setupStream(){

var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
"127.0.0.1" as CFString,
80,
&readStream,
&writeStream)

inputStream = readStream!.takeRetainedValue()
outputStream = writeStream!.takeRetainedValue()

inputStream.delegate = self

inputStream.schedule(in: .current, forMode: .common)
outputStream.schedule(in: .current, forMode: .common)

// SETTING SSL HERE
inputStream.setProperty(kCFStreamSocketSecurityLevelNegotiatedSSL, forKey: Stream.PropertyKey.socketSecurityLevelKey)
outputStream.setProperty(kCFStreamSocketSecurityLevelNegotiatedSSL, forKey: Stream.PropertyKey.socketSecurityLevelKey)
// END SSL SETUP

inputStream.open()
outputStream.open()

}
}

最佳答案

我一直在审查图书馆 SocketRocket检查您的代码。该库是用 Objective-C 实现的,但您可以将其用作引用。

在该库中,在更新安全流选项的代码中,我观察到它仅更新了 outputStream 的 kCFStreamSocketSecurityLevelNegotiedSSL。

- (void)_updateSecureStreamOptions {
if (_secure) {
NSMutableDictionary *SSLOptions = [[NSMutableDictionary alloc] init];

/*ONLY FOR OUTPUT STREAM*/
[_outputStream setProperty:(__bridge id)kCFStreamSocketSecurityLevelNegotiatedSSL forKey:(__bridge id)kCFStreamPropertySocketSecurityLevel];

// If we're using pinned certs, don't validate the certificate chain
if ([_urlRequest SR_SSLPinnedCertificates].count) {
[SSLOptions setValue:@NO forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
}

#if DEBUG
self.allowsUntrustedSSLCertificates = YES;
#endif

if (self.allowsUntrustedSSLCertificates) {
[SSLOptions setValue:@NO forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
SRFastLog(@"Allowing connection to any root cert");
}

[_outputStream setProperty:SSLOptions
forKey:(__bridge id)kCFStreamPropertySSLSettings];
}

_inputStream.delegate = self;
_outputStream.delegate = self;

[self setupNetworkServiceType:_urlRequest.networkServiceType];
}

希望对您有所帮助。

关于swift - TCP 套接字流和 SSL 与 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com