昨天在os x上写了个网络通信的小程序http://blog.youkuaiyun.com/zj510/article/details/8934312,今天在iphone也试了一下。
使用了Cocoa中的NSStream.代码相当的简单。
- (void)Work_Thread:(NSURL *)url
{
NSString* strHost = [url host];
int port = [[url port] integerValue];
[NSStream getStreamsToHostNamed:strHost port: port inputStream:&readStream outputStream:&writeStream];
[readStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];
[readStream setDelegate:self];
[readStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[readStream open];
NSLog(@"VOIP stream, %x", readStream);
[writeStream open];
[writeStream write:"abcd" maxLength:4];
NSInputStream* iStream;
[NSStream getStreamsToHostNamed:strHost port:port inputStream:&iStream outputStream:nil];
[iStream setDelegate: self];
[iStream scheduleInRunLoop