Benches Uncategorized

1

Named Pipes using mkfifo and Cocoa

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I have a C++ application that uses named pipes by calling mkfifo and I would like to use them to connect to a Cocoa app. I have taken a look at NSPipe but there isn't an option to connect to a named pipe. Does anyone know how to connect NSPipes or something else I can tie into the NSNotificationCenter that can talk to named pipes?

Thank you for your time,

Tom

1 reply

Sign in or join Beachball Help to reply.

  1. 0

    tdesrochers

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    -(void)SomeFunc

    {

    const char * path = "/tmp/tom21";

    if(mkfifo(path, 0666) == -1 && errno !=EEXIST){

    NSLog(@"Unable to open the named pipe %c", path);

    }

    NSFileHandle * filehandleForReading;

    int fd = open(path, O_RDWR | O_NDELAY);

    filehandleForReading = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc: YES];

    NSNotificationCenter *nc;

    nc = [NSNotificationCenter defaultCenter];

    [nc removeObserver:self];

    [nc addObserver:self

    selector:@selector(dataReady:)

    name:NSFileHandleReadCompletionNotification

    object:filehandleForReading];

    [filehandleForReading readInBackgroundAndNotify];

    }

    And then here is the func that gets called by the Notification server

    - (void)dataReady:(NSNotification *)n

    {

    NSData *d;

    d = [[n userInfo] valueForKey:NSFileHandleNotificationDataItem];

    NSLog(@"dataReady:%d bytes", [d length]);

    if ([d length]) {

    [self appendData:d];

    }

    //Tell the fileHandler to asychronusly report back

    [filehandleForReading readInBackgroundAndNotify];

    }