CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)distantHost, port, &readStream, &writeStream);
if (readStream && writeStream)
{
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
}
CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);
int result;
CFDataRef fdData;
result = -1;
if (writeStream != nil) {
fdData = CFWriteStreamCopyProperty((CFWriteStreamRef) writeStream, kCFStreamPropertySocketNativeHandle);
if (fdData != nil) {
NSLog(@"fdData is not nil");
assert(CFDataGetLength(fdData) == sizeof(result));
result = *(int *) CFDataGetBytePtr(fdData);
CFRelease(fdData);
}
else
NSLog(@"fdData is nil");
}
readStream and writeStream are not null because further in the code I can read and write from them. distantHost is still an NSString containing either a domain name or an IP address. I open my stream with CFReadStreamOpen and CFWriteStreamOpen (Is it OK?) but when I go through the CFWriteStreamCopyProperty (or CFReadStreamCopyProperty) returns a nil because it logs "fdData is nil". I can't understand why fdData is nil. Am I doing something wrong?