我的 iOS 应用程序包含 2 个版本:服务器和客户端。服务器通过 Bonjour 发布服务并等待客户端。客户端搜索该服务并连接到服务器。之后,所有通信都通过 CFSockets 实现。
这是我为此类通信设置流的代码:
- (BOOL)setupSocketStreams {
if ( readStream == nil || writeStream == nil ) {
return NO;
}
incomingDataBuffer = [[NSMutableData alloc] init];
outgoingDataBuffer = [[NSMutableData alloc] init];
// Turning SSL on
NSDictionary *sslSettings = [NSDictionary dictionaryWithObjectsAndKeys:(id)kCFBooleanFalse, (id)kCFStreamSSLValidatesCertificateChain, nil];
CFReadStreamSetProperty(readStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, sslSettings);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, sslSettings);
CFReadStreamSetProperty(
readStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue
);
CFWriteStreamSetProperty(
writeStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue
);
CFOptionFlags registeredEvents =
kCFStreamEventOpenCompleted |
kCFStreamEventHasBytesAvailable |
kCFStreamEventCanAcceptBytes |
kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred
;
CFStreamClientContext ctx = {
0, // version
self, // info
NULL, // retain callback
NULL, // release callback
NULL // copyDescription callback
};
CFReadStreamSetClient(
readStream,
registeredEvents,
readStreamEventHandler,
&ctx
);
CFWriteStreamSetClient(
writeStream,
registeredEvents,
writeStreamEventHandler,
&ctx
);
CFReadStreamScheduleWithRunLoop(
readStream,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes
);
CFWriteStreamScheduleWithRunLoop(
writeStream,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes
);
if ( ! CFReadStreamOpen(readStream) || ! CFWriteStreamOpen(writeStream)) {
[self close];
return NO;
}
if ([self.delegate respondsToSelector:@selector(establishedConnection:)]) {
[self.delegate establishedConnection:self];
}
return YES;
}
如果没有在此代码上打开 SSL 的部分工作正常,但否则我会收到错误消息:
Error:
The operation couldn’t be completed. (OSStatus error -9800.)
会是什么问题?
谢谢!