[英]How to create a NSInputStream/NSOutputStream pair by accepting a server socket?
I know that you can create a NSInputStream
and NSOutputStream
pair (toll-free bridged to CFReadStream
, CFWriteStream
) by opening a TCP client connection using CFStreamCreatePairWithSocketToHost
according to the Stream Programming Guide . 我知道您可以根据流编程指南通过使用
CFStreamCreatePairWithSocketToHost
打开TCP客户端连接来创建NSInputStream
和NSOutputStream
对(将免费电话桥接到CFReadStream
, CFWriteStream
)。
How can one create a TCP server that listens on a port and accepts a connection as a NSInputStream
and NSOutputStream
pair? 如何创建一个侦听端口并以
NSInputStream
和NSOutputStream
对的形式接受连接的TCP 服务器 ?
The RemoteCurrency sample project provides an example of this in QServer.m (previously known as TCPServer.m). RemoteCurrency示例项目在QServer.m (以前称为TCPServer.m)中提供了一个示例。 The sample code is actually for setting up Bonjour advertising, but that part can be excluded for a simple TCP server.
该示例代码实际上是用于设置Bonjour广告的,但是对于简单的TCP服务器,该部分可以排除。
You create a socket with your own accept callback by calling CFSocketCreate
, bind and listen to a port by calling CFSocketSetAddress
, and start getting accept callbacks by calling CFSocketCreateRunLoopSource
followed by CFRunLoopAddSource
. 您可以通过调用创建自己接受回调插座
CFSocketCreate
, 绑定和调用监听端口CFSocketSetAddress
,并开始获得通过调用接受回调CFSocketCreateRunLoopSource
其次CFRunLoopAddSource
。
Then, in the accept callback, you create NSInputStream
/ NSOutputStream
pair from the connection handle using CFStreamCreatePairWithSocket
, set the input stream's delegate and start receiving recv callbacks by calling scheduleInRunLoop:forMode:
, and then open
both streams to start using them. 然后,在accept回调中,使用
CFStreamCreatePairWithSocket
从连接句柄创建NSInputStream
/ NSOutputStream
对,设置输入流的委托,并通过调用scheduleInRunLoop:forMode:
开始接收recv回调,然后open
两个流以开始使用它们。
Using NSInputStream and NSOutputStream abstractions (as opposed to creating a new thread and calling the standard UNIX functions socket
, bind
, listen
, accept
, send
, recv
) allows one to easily receive network events in the same NSRunLoop as the rest of the run-loop based APIs on OSX. 使用NSInputStream和NSOutputStream抽象(而不是创建一个新线程并调用标准UNIX函数
socket
, bind
, listen
, accept
, send
, recv
),可以轻松地在与其余运行循环相同的NSRunLoop中接收网络事件。基于OSX的API。
I have a sample app that you can download that creates a pair of NSStreams connected through a socket. 我有一个示例应用程序,您可以下载该应用程序,该应用程序创建通过套接字连接的一对NSStreams。 It then streams picture-perfect, real-time video through the sockets using the camera on an iOS device:
然后,它使用iOS设备上的摄像头通过插槽流传输完美图片的实时视频:
The code is concise, and simple-to-follow (ignore the extra files in the project; NavController.h/m and PeerConnectionViewController.h/m and CameraViewController.h/m are the only three files of relevance): 代码简洁明了,并且易于遵循(忽略项目中的多余文件; NavController.h / m和PeerConnectionViewController.h / m和CameraViewController.h / m是仅有的三个相关文件):
https://app.box.com/s/fnim5ril4mzu518wj98zy3ah8l83jayv https://app.box.com/s/fnim5ril4mzu518wj98zy3ah8l83jayv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.