简体   繁体   English

如何在Objective-c中将数据从NSOutputStream传递到NSInputStream

[英]how to pipe data from NSOutputStream to NSInputStream in objective-c

I have 2 libraries that I want to integrate and make them talk to each other. 我有两个我想要集成的库,让它们相互通信。 Each of them listen on their own input and output streams. 他们每个人都听自己的输入和输出流。 Library 1 will be the transport layer for library 2. 库1将是库2的传输层。

Case 1: Library 1 receives data on its input stream I want to write data on another dummy outputstream which will be piped to the input stream on library 2. 情况1:库1接收其输入流上的数据我想在另一个虚拟输出流上写入数据,该输出流将通过管道传输到库2上的输入流。

Case 2: Library 2 wants to send some data, so it will write data onto its outputstream. 案例2:库2想要发送一些数据,因此它会将数据写入其输出流。 This should be piped to a dummy input stream from where data will be read and written onto the output stream of library 1. 这应该通过管道传输到虚拟输入流,数据将被读取并写入库1的输出流。

How do i create pipes for these NSStreams in objective-c ? 如何在objective-c中为这些NSStream创建管道?

Thanks in advance for your inputs. 提前感谢您的意见。

Here is how to create a simple pipe: 以下是创建简单管道的方法:

CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;

CFStreamCreateBoundPair(NULL, &readStream, &writeStream, 4096);

NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;

What you write to outputStream will be readable from inputStream. 您写入outputStream的内容将从inputStream中读取。

Swift version of Gabriele Mondada's answer Swift版本的Gabriele Mondada的答案

    var readStream:Unmanaged<CFReadStream>?
    var writeStream:Unmanaged<CFWriteStream>?

    CFStreamCreateBoundPair(nil, &readStream, &writeStream, 4096)

    let inputStream:NSInputStream = readStream!.takeRetainedValue()
    let outputStream:NSOutputStream = writeStream!.takeRetainedValue()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用NSStream,NSInputStream和NSOutputStream从C#应用程序发送和接收数据吗? - Can I use NSStream, NSInputStream, and NSOutputStream to send and receive data from a C# application? 关闭NSOutputstream和NSInputstream - Closing an NSOutputstream & NSInputstream NSInputStream和NSOutputStream问题 - NSInputStream and NSOutputStream problems 如何通过Objective-C从JSON的键解析数据? - How to parse the data from json's key by objective-c? 如何在Objective-C中解析文本文件中的JSON数据 - How to parse JSON data from textual file in Objective-C 如何将数据从 javascript 传递到 IOS Objective-c 中的 Z5A98E2840FD0141780D854E48C608D? - how to pass data from javascript to IOS Objective-c in webview? 如何使用NSInputStream和NSOutputStream读取和写入音频文件 - How to read and write audio file using NSInputStream and NSOutputStream 如何在Objective-C中将数据从反向地理编码保存到CoreData? - How to save data from reversegeocoding to CoreData in Objective-C? 如何在Objective-C中使用来自本地主机的JSON数据? - How to use json data from localhost in objective-c? 如何在流打开后将NSStream(NSInputStream / NSOutputStream)转换为SSL? - How to convert NSStream (NSInputStream / NSOutputStream) to SSL after stream opened?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM