Problems in getting data from CFStreamCreatePairWithSocketToHost

Posted by gkedmi on Stack Overflow See other posts from Stack Overflow or by gkedmi
Published on 2010-06-09T07:37:23Z Indexed on 2010/06/09 7:42 UTC
Read the original article Hit count: 367

Filed under:
|
|
|

Hi I'm building an iPhoe app with a socket to a PC app , I need to get an image from this PC app. It's my first time using "CFStreamCreatePairWithSocketToHost".After I establish the socket with "NSOperation" I call


CFStreamClientContext streamContext = {0, self, NULL, NULL, NULL};
BOOL success = CFReadStreamSetClient(myReadStream, kMyNetworkEvents,MyStreamCallBack,&streamContext);
CFReadStreamScheduleWithRunLoop(myReadStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
then I call

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); }

and then when I get "kCFStreamEventHasBytesAvailable" I use

while (CFReadStreamHasBytesAvailable(myReadStream))
{
   CFReadStreamRead(myReadStream, readBuffer, 1000);
//and buffer the the bytes
}
It's unpredictable , sometimes I get the whole picture , sometime I got just part of it , and I can't understand what make the different. can someone has an idea what is wrong here? thanks

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c