简体   繁体   English

如何通过接受服务器套接字来创建NSInputStream / NSOutputStream对?

[英]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客户端连接来创建NSInputStreamNSOutputStream对(将免费电话桥接到CFReadStreamCFWriteStream )。

How can one create a TCP server that listens on a port and accepts a connection as a NSInputStream and NSOutputStream pair? 如何创建一个侦听端口并以NSInputStreamNSOutputStream对的形式接受连接的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. 使用NSInputStreamNSOutputStream抽象(而不是创建一个新线程并调用标准UNIX函数socketbindlistenacceptsendrecv ),可以轻松地在与其余运行循环相同的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.

相关问题 使用NSOutputStream通过套接字发送到服务器时,用换行符分隔数据(JSON) - Delimit data (JSON) with newline when sending to server over socket using NSOutputStream TCP服务器不接受第二个套接字发送 - TCP server not accepting second socket send TCP套接字-服务器不接受任何UNIX连接 - TCP Socket - Server not accepting any connections UNIX 套接字服务器在同一进程上一次仅接受一个连接 - Socket Server accepting only one connection at a time on the same process 接受新连接时,PHP TCP套接字服务器挂起 - PHP TCP socket server hangs when accepting a new connection Java TCP 服务器套接字在不同端口上接受多个客户端 - Java TCP server socket accepting multiple clients on different ports 如何继续在异步套接字上接受数据? - How do I keep accepting data on my async socket? NSInputStream无法从TCP连接读取[TCP-Server] - NSInputStream can't read from TCP Connection [TCP-Server] 如何将java套接字连接到远程服务器套接字 - How to connect java socket to remote server socket 如何在 spring boot 中创建一个有效的 TCP Server 套接字以及如何处理传入的消息? - How to create a working TCP Server socket in spring boot and how to handle the incoming message?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM