本文以及相关的系列文章是我总结的iOS网络开发方面的知识点,本文是第二篇,主要分析了Cocoa Streams中的几个重要类

Cocoa Streams实际上是Objective-C对CFNetwork的简单封装,主要包括了三个类:NSStream, NSInputStream, and NSOutputStream。本部分的接口接口比較简单,使用方法一目了然。

我在这里就仅仅列出接口,方便查阅。

对CFNnework不明确的看IOS研究之网络编程(一)-CFNetwork使用具体解释

NSStream接口例如以下:

@interface NSStream : NSObject
- (void)open;
- (void)close;

- (id <NSStreamDelegate>)delegate;
- (void)setDelegate:(id <NSStreamDelegate>)delegate;
// By default, a stream is its own delegate, and subclassers of NSInputStream and NSOutputStream must maintain this contract. [someStream setDelegate:nil] must restore this behavior. As usual, delegates are not retained.

- (id)propertyForKey:(NSString *)key;
- (BOOL)setProperty:(id)property forKey:(NSString *)key;

- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;

- (NSStreamStatus)streamStatus;
- (NSError *)streamError;
@end

@protocol NSStreamDelegate <NSObject>
@optional
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode;
@end

#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))//不能在ios上使用
@interface NSStream (NSSocketStreamCreationExtensions)
+ (void)getStreamsToHost:(NSHost *)host port:(NSInteger)port inputStream:(NSInputStream **)inputStream outputStream:(NSOutputStream **)outputStream;
@end
#endif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@interface
NSStream :
NSObject  
- (void)open;  
-
(void)close;  
  
-
(id
<NSStreamDelegate>)delegate;  
- (void)setDelegate:(id
<NSStreamDelegate>)delegate;  
    // By default, a stream is its own delegate, and subclassers of NSInputStream and NSOutputStream must maintain this contract. [someStream
setDelegate:nil] must restore this behavior. As usual, delegates are not retained.  
  
-
(id)propertyForKey:(NSString *)key;  
- (BOOL)setProperty:(id)property
forKey:(NSString *)key;  
  
- (void)scheduleInRunLoop:(NSRunLoop
*)aRunLoop forMode:(NSString *)mode;  
-
(void)removeFromRunLoop:(NSRunLoop *)aRunLoop
forMode:(NSString *)mode;  
  
-
(NSStreamStatus)streamStatus;  
- (NSError *)streamError;  
@end  
  
@protocol
NSStreamDelegate
<NSObject>  
@optional  
-
(void)stream:(NSStream *)aStream
handleEvent:(NSStreamEvent)eventCode;  
@end  
  
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))//不能在ios上使用  
@interface
NSStream (NSSocketStreamCreationExtensions)  
+ (void)getStreamsToHost:(NSHost
*)host port:(NSInteger)port
inputStream:(NSInputStream **)inputStream
outputStream:(NSOutputStream **)outputStream;  
@end  
#endif  

NSInputStream和NSOutputStream都是继承NSStream,接口例如以下

@interface NSInputStream : NSStream
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len;
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len;
- (BOOL)hasBytesAvailable;
@end

@interface NSOutputStream : NSStream
- (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)len;
- (BOOL)hasSpaceAvailable;
@end

1
2
3
4
5
6
7
8
9
10
@interface
NSInputStream
: NSStream  
- (NSInteger)read:(uint8_t
*)buffer maxLength:(NSUInteger)len;  
-
(BOOL)getBuffer:(uint8_t **)buffer
length:(NSUInteger *)len;  
- (BOOL)hasBytesAvailable;  
@end  
  
@interface
NSOutputStream
: NSStream  
- (NSInteger)write:(const
uint8_t *)buffer
maxLength:(NSUInteger)len;  
-
(BOOL)hasSpaceAvailable;  
@end  

但这里getStreamsToHost方法,在iOS上不可用。那么在iOS上改怎样初始化NSStream呢?这事实上是iOS的一个小bug。苹果已经给出了答案,须要我们自己为NSStream加入一个类目:

IOS研究之网络编程(二)-Cocoa Streams使用具体解释的更多相关文章

  1. iOS开发之网络编程--5、NSURLSessionUploadTask+NSURLSessionDataDelegate代理上传

    前言:关于NSURLSession的主要内容快到尾声了,这里就讲讲文件上传.关于文件上传当然就要使用NSURLSessionUploadTask,这里直接讲解常用的会和代理NSURLSessionDa ...

  2. iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>

    前言:根据前篇<iOS开发之网络编程--2.NSURLSessionDownloadTask文件下载>或者<iOS开发之网络编程--3.NSURLSessionDataTask实现文 ...

  3. iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)

    前言:使用NSURLSessionDownloadTask满足不这个需要离线断点续传的下载需求,所以这里就需要使用NSURLSessionDataTask的代理方法来处理下载大文件,并且实现离线断点续 ...

  4. iOS开发之网络编程--2、NSURLSessionDownloadTask文件下载

    本文内容大纲: 1.回顾NSURLSessionTask 2.NSURLSessionDownloadTask大文件之block下载 3.NSURLSessionDownloadTask大文件之代理方 ...

  5. iOS开发之网络编程--使用NSURLConnection实现文件上传

    前言:使用NSURLConnection实现文件上传有点繁琐.    本文并没有介绍使用第三方框架上传文件. 正文: 这里先提供用于编码测试的接口:http://120.25.226.186:3281 ...

  6. iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄

    前言:本篇讲解,在前篇iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载的基础上,使用输出流代替文件句柄实现大文件断点续传.    在实际开发中,输入输出流用的比较少,但 ...

  7. iOS开发之网络编程--使用NSURLConnection实现大文件下载

    主要思路(实现下载数据分段写入缓存中) 1.使用NSURLConnectionDataDelegate以及代理方法.2.在成功获取响应的代理方法中,获得沙盒全路径,并在该路径下创建空文件和文件句柄.3 ...

  8. iOS开发之网络编程--小文件下载

    文件下载方式: 如果下载的文件比较小,下载方式: 直接用NSData的 +(id)dataWithContentsOfURL:(NSURL*)url; 利用NSURLConnection发送一个HTT ...

  9. Linux网络编程(二)

    Linux网络编程(二) 使用多进程实现服务器并发访问. 采用多进程的方式实现服务器的并发访问的经典范例. 程序实现功能: 1.客户端从标准输入读入一行文字,发送到服务器. 2.服务器接收到客户端发来 ...

随机推荐

  1. 【HDOJ5519】Kykneion asma(状压DP,容斥)

    题意:给定n和a[i](i=0..4),求所有n位5进制数中没有前导0且i出现的次数不超过a[i]的数的个数 2<=n<=15000,0<=a[i]<=3e4 思路:设f(n, ...

  2. [LeetCode] Remove Duplicates from Sorted List 链表

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. [SaltStack] Salt高可用和负载均衡部署

    Saltstack HA部署 Salt官网是有HA/Rebalance/failover解决方案的, 但版本必须是2014.7, 目前该版本还没有release, 从官网下载的源码包安装使用测试起来一 ...

  4. vmware tools安装过程

    每次通过vmware安装Ubuntu的时候,总是会多多少少出点问题.好容易披荆斩棘把镜像安好了,然而屏幕却只有小小一个,不能显示大屏,我就知道肯定是缺少了vmware tools.于是点击左上方菜单中 ...

  5. Python学习杂记_7_文件操作

    文件操作 Python3用open()方法打开文件并返回文件句柄,有了文件句柄就可以对文件进行各种操作了. 打开文件: open(“文件名” , 打开方式)            如: f=open( ...

  6. hexo安装问题解决方法

    常见错误 { [Error: Cannot find module './build/Release/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND' ...

  7. DB2时间函数 实现 时间加减

    时间加减:后边记得跟上时间类型如day.HOUR TIMESTAMP ( TIMESTAMP(DEF_TIME)+1 day)+18 HOUR   DB2时间函数是我们最常见的函数之一,下面就为您介绍 ...

  8. Codeforces 691E Xor-sequences(矩阵加速DP)

    题目链接 Xor-sequences 利用矩阵加速. 先预处理出当序列长度为$2$的时候的方案数. 也就是说这个序列起点是$a[i]$终点是$a[j]$且中间没有任何元素. 但是所求的$k$很大,序列 ...

  9. BZOJ1457 棋盘游戏

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1457 这题神奇一些就在于这题的胜利条件不是拿走最后一张牌了而是走到(0,0). 然后就需要 ...

  10. Timeout watchdog using a standby thread

    http://codereview.stackexchange.com/questions/84697/timeout-watchdog-using-a-standby-thread he simpl ...