我正在构建一个带有 PC 应用程序套接字的 iPhoe 应用程序,我需要从此 PC 应用程序获取图像。这是我第一次使用“CFStreamCreatePairWithSocketToHost”。在使用“NSOperation”建立套接字后,我调用
CFStreamClientContext streamContext = {0, self, NULL, NULL, NULL};
BOOL success = CFReadStreamSetClient(myReadStream, kMyNetworkEvents,MyStreamCallBack,&streamContext);
CFReadStreamScheduleWithRunLoop(myReadStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
然后我打电话
CFWriteStreamWrite(myWriteStream, &writeBuffer, 3);
// Open read stream.
if (!CFReadStreamOpen(myReadStream)) {
// Notify error
}
.
.
.
while(!cancelled && !finished) {
SInt32 result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, NO);
if (result == kCFRunLoopRunStopped || result == kCFRunLoopRunFinished) {
break;
}
if (([NSDate timeIntervalSinceReferenceDate] - _lastRead) > MyConnectionTimeout) {
// Call timed out
cancelled = YES;
break;
}
// Also handle stream status
CFStreamStatus status = CFReadStreamGetStatus(myReadStream);
}
然后当我得到“kCFStreamEventHasBytesAvailable”时,我使用
while (CFReadStreamHasBytesAvailable(myReadStream))
{
CFReadStreamRead(myReadStream, readBuffer, 1000);
//and buffer the the bytes
}
这是不可预知的,有时我了解整个情况,有时我只了解其中的一部分,我不明白是什么让不同。有人可以知道这里有什么问题吗?谢谢