named pipe in cocoa
I need to have a high performance communication between 2 applications. I tried AppleEvent but it is not really a good option. I thought to use a named pipe but I do not know how to use them in COCOA.
Thanks in advance for y开发者_运维百科our help :)
There's no special way to do it in Cocoa. You'd basically make the fifo and once you had that, your process could then read it/write it using the standard Cocoa wrappers like NSFileHandle
. You could check for its existence with NSFileManager
or whatever, but you'd still have to call mkfifo(2)
at some point to create the named pipe.
So:
if( mkfifo("/tmp/my_named_pipe", 0644) == -1 ) {
// some error handling
abort();
}
// Open and use the fifo as you would any file in Cocoa, but remember that it's a FIFO
NSFileHandle* fifoIN = [NSFileHandle fileForReadingAtPath:@"/tmp/my_named_pipe"];
Personally, I would recommend using either a BSD or Unix socket instead. There's lots of Framework support for sockets, sometimes a bi-directional channel is more useful as well, and you wouldn't have to worry about whether the reader or the writer is ready first.
There are alternatives to named pipes:
- Sockets, as Jason Coco already mentioned.
- Mach port messages. Likely to be the highest-performance option, since this is pretty much the feature Mach was made for.
- Distributed objects. Implemented on top of ports (and I believe you can use either socket ports or Mach ports), this is the easiest option to use, since you send messages to “distant” objects (vended by other processes) the same way as you send them to local objects (created in your own process).
精彩评论