Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

Case 2: Library 2 wants to send some data, so it will write data onto its outputstream. This should be piped to a dummy input stream from where data will be read and written onto the output stream of library 1.

How do i create pipes for these NSStreams in objective-c ?

Thanks in advance for your inputs.

like image 737
Sharath Ambati Avatar asked Jan 30 '15 19:01

Sharath Ambati


2 Answers

Swift version of Gabriele Mondada's answer

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

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

    let inputStream:NSInputStream = readStream!.takeRetainedValue()
    let outputStream:NSOutputStream = writeStream!.takeRetainedValue()
like image 190
Dale Avatar answered Sep 30 '22 19:09

Dale


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.

like image 36
Gabriele Mondada Avatar answered Sep 30 '22 20:09

Gabriele Mondada