IOS研究之网络编程(二)-Cocoa Streams使用具体解释
本文以及相关的系列文章是我总结的iOS网络开发方面的知识点,本文是第二篇,主要分析了Cocoa Streams中的几个重要类
Cocoa Streams实际上是Objective-C对CFNetwork的简单封装,主要包括了三个类:NSStream, NSInputStream, and NSOutputStream。本部分的接口接口比較简单,使用方法一目了然。
我在这里就仅仅列出接口,方便查阅。
对CFNnework不明确的看IOS研究之网络编程(一)-CFNetwork使用具体解释
NSStream接口例如以下:
- (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,接口例如以下
- (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使用具体解释的更多相关文章
- iOS开发之网络编程--5、NSURLSessionUploadTask+NSURLSessionDataDelegate代理上传
前言:关于NSURLSession的主要内容快到尾声了,这里就讲讲文件上传.关于文件上传当然就要使用NSURLSessionUploadTask,这里直接讲解常用的会和代理NSURLSessionDa ...
- iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>
前言:根据前篇<iOS开发之网络编程--2.NSURLSessionDownloadTask文件下载>或者<iOS开发之网络编程--3.NSURLSessionDataTask实现文 ...
- iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)
前言:使用NSURLSessionDownloadTask满足不这个需要离线断点续传的下载需求,所以这里就需要使用NSURLSessionDataTask的代理方法来处理下载大文件,并且实现离线断点续 ...
- iOS开发之网络编程--2、NSURLSessionDownloadTask文件下载
本文内容大纲: 1.回顾NSURLSessionTask 2.NSURLSessionDownloadTask大文件之block下载 3.NSURLSessionDownloadTask大文件之代理方 ...
- iOS开发之网络编程--使用NSURLConnection实现文件上传
前言:使用NSURLConnection实现文件上传有点繁琐. 本文并没有介绍使用第三方框架上传文件. 正文: 这里先提供用于编码测试的接口:http://120.25.226.186:3281 ...
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
前言:本篇讲解,在前篇iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载的基础上,使用输出流代替文件句柄实现大文件断点续传. 在实际开发中,输入输出流用的比较少,但 ...
- iOS开发之网络编程--使用NSURLConnection实现大文件下载
主要思路(实现下载数据分段写入缓存中) 1.使用NSURLConnectionDataDelegate以及代理方法.2.在成功获取响应的代理方法中,获得沙盒全路径,并在该路径下创建空文件和文件句柄.3 ...
- iOS开发之网络编程--小文件下载
文件下载方式: 如果下载的文件比较小,下载方式: 直接用NSData的 +(id)dataWithContentsOfURL:(NSURL*)url; 利用NSURLConnection发送一个HTT ...
- Linux网络编程(二)
Linux网络编程(二) 使用多进程实现服务器并发访问. 采用多进程的方式实现服务器的并发访问的经典范例. 程序实现功能: 1.客户端从标准输入读入一行文字,发送到服务器. 2.服务器接收到客户端发来 ...
随机推荐
- 关于微信小程序post请求数据的坑
在post请求数据的时候,发现数据没有发送给后台,需要在请求头里加"Content-Type": "application/x-www-form-urlencoded&q ...
- jQuery1.4与json格式兼容问题
原文发布时间为:2010-10-10 -- 来源于本人的百度文章 [由搬家工具导入] 原来使用jQuery1.3.2编写的代码,更换到1.4.2后,使用jQuery.ajax()加载的json文件,不 ...
- 调用Outlook发送邮件
#region 查找与指定文件关联在一起的程序的文件名 /// <summary> /// 查找与指定文件关联在一起的程序的文件名 /// </summary> /// < ...
- WIN8.1安装 .net framework 3.5
1.虚拟光驱加载安装镜像 2.以管理员权限运行cmd 3.输入以下命令: dism.exe /online /enable-feature /featurename:NetFX3 /Source:X: ...
- usb 2.0 operation mode
一般來說 USB 的通訊結構有如 Server/Client,以 PC 上的情形為例,位於主機上的 USB 裝置稱為『USB Host』,我們可以在上面外接上數個裝置(與 USB Host 相連的裝置 ...
- Bioconda安装与使用
1. Bioconda是一个自动化管理生物信息软件的工具,就像APPstore.360软件管家一样. Bioconda的优点是安装简单,各个软件依赖的环境一同打包且相互隔离,非常适合在服务器中建立自 ...
- Selenium 2.0自动化测试
http://blog.sina.com.cn/s/blog_b6142fb401017oo6.html http://www.cnblogs.com/halia/p/3562132.html?utm ...
- K皇后问题递归解法
#include<iostream> #include<cmath> #include<ctime> using namespace std; bool che ...
- VS2017使用
1. 用了一段时间的layui,发现官网的升级的功能很好用,于是把自己本地项目的layui升级到的高版本.按照官网上的代码几乎一模一样,但是功能就是出不来,之后发现本地虽然是高版本但是生成的脚本依然是 ...
- 记录一次(xheditor-1.1.6-zh-cn.min.js)的错误:Cannot read property 'match' of undefined的问题解决
由于使用了xheditor富文本框,且这个版本是2011年开发的系统,当时只有IE8,所以一切正常. 但是问题来了,今天使用IE11测试和谷歌浏览器测试,发现一直报这个错误: 且google了一下,没 ...