如何将CFReadStreamRef转换为NSInputStream?

时间:2014-08-10 01:19:23

标签: ios sockets network-programming

    - (void)connection:(NSString*)serviceName forIpAddress:(NSString *)ipAddress
       forPort:(NSString *)portNo
  {
if(inputStream && outputStream)
    //[self close];

    NSString *urlString = [NSString stringWithFormat:@"http://%@",ipAddress];

NSURL *website = [NSURL URLWithString:urlString];

if (!website) {
    NSLog(@"%@ is not a valid URL", website);
}
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], [portNo
                                                                       intValue], &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket,
                        kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket,
                         kCFBooleanTrue);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[self open];

}

在NNString,NSInputStream,NSoutputStream处获取ARC错误。 错误说.. c指针类型CFreadStreamRef到c指针类型NSinput流需要桥接转换。

1 个答案:

答案 0 :(得分:0)

NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;

请参见Setting Up Socket Streams编程指南。