真的iPhone设备上有'getStreamsToHost'吗? [英] Is there anything like 'getStreamsToHost' on real iPhone device?

查看:52
本文介绍了真的iPhone设备上有'getStreamsToHost'吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用apple的示例代码将NSOutputStream写入服务器:

I want to write an NSOutputStream to a server with apple's sample code:


NSURL *website = [NSURL URLWithString:str_IP];
NSHost *host = [NSHost hostWithName:[website host]];
[NSStream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];
[oStream retain];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream open];

这些代码在iPhone模拟器上运行良好,但是当我将它构建到真实设备时。弹出两个警告。问题是:

These codes works well on the iPhone simulator, but when I build it to real device. Two warnings pop up. The problem is:

1)类NSHost不属于iphone os库

1)class NSHost doesn't belong to iphone os library

2)getStreamsToHost是找不到

2)getStreamsToHost is not found either

对可以在真实设备上使用的替代方法或类的任何建议?

Any suggestions for the alternative method or class which can be used on real device?

推荐答案

因为CFWriteStream是免费桥接到NSOutputStream你可以使用CFStreamCreatePairWithSocketToHost获取你的流对:

Since CFWriteStream is toll-free bridged to NSOutputStream you can use CFStreamCreatePairWithSocketToHost to get your stream pair:

CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)host, port, &readStream, &writeStream);
if (readStream && writeStream) {
    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

    inputStream = (NSInputStream *)readStream;
    [inputStream retain];
    [inputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];

    outputStream = (NSOutputStream *)writeStream;
    [outputStream retain];
    [outputStream setDelegate:self];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream open];
}

if (readStream)
    CFRelease(readStream);

if (writeStream)
    CFRelease(writeStream);

这篇关于真的iPhone设备上有'getStreamsToHost'吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆