istream和ostream的iphone功能

时间:2011-05-31 05:16:35

标签: iphone nsstream nsinputstream nsoutputstream

任何人都可以在下面的代码中告诉我istream和ostream的功能,我怎样才能声明istream和ostream,因为我从网上选择了代码,但对代码知之甚少

NSString *urlStr = @"192.168.178.26";
if (![urlStr isEqualToString:@""]) {
    NSURL *website = [NSURL URLWithString:urlStr];
    if (!website) {
        NSLog(@"%@ is not a valid URL");
        return;
    }
    NSHost *host = [NSHost hostWithName:[website host]];
    [NSStream getStreamsToHost:host port:3258 inputStream:&iStream  outputStream:&oStream];
    [iStream retain];
    [oStream retain];
    [iStream setDelegate:self];
    [oStream setDelegate:self];
    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [iStream open];
    [oStream open];
    /* ... */
}

1 个答案:

答案 0 :(得分:0)

根据NSStream reference pageiStream应为NSInputStream*指针,oStream应为NSOutputStream*&表示您将iStreamoStream的地址传递到-getStreamsToHost:...方法。这是您通过C(或Objective-C)中的引用传递的方式。然后,该方法将尝试打开与您指定的主机的连接,如果成功,将为数据流创建流对象(iStream)和(oStream)该主机并将在这些主机中返回它们变量