1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSMachPort Vs NSMessagePort

Discussion in 'Mac Programming' started by sivaprakash, Apr 25, 2011.

  1. macrumors member


    What is the difference between NSMachPort and NSMessagePort ? And When should I use which ?

  2. jiminaus, Apr 26, 2011
    Last edited: Apr 26, 2011

    macrumors 65816


    NSMachPort implementes NSPort using a Mach port. It's very efficient for IPC on the same machine. If you want to do IPC across different machines, you must use NSSocketPort.

    NSMessagePort is also limited to same machine IPC like NSMachPort. I don't know what communications method it uses, I've never used or explored it.

    I quote from the NSMessagePort class reference.
    EDIT: I did some more reading around this. It seems NSMessagePort was added when NextSTEP evolved into OPENSTEP because Sun couldn't implement NSMachPort on Solaris. So it seems NSMessagePort was meant to be a cross-platform alternative to NSMachPort.

    GNUStep doesn't provide NSMachPort. So if you want your code to be compilable under both Cocoa and GNUSTEP, you'd need to ignore Apple's advise and use NSMessagePort.

    But really, you should only use NSPort* variables. That way your code will with be portable both across Cocoa and GNUSTEP, and across local and remote ports (assuming you have properly designed distributed objects, eg no fine-grained messaging).
  3. macrumors member

    That simplified my search !! Thanks much.

    Can make NSMachPort Server Program as a Daemon thread? so that multiple applications can talk to that service?
  4. macrumors 65816


    Yes, they could be used directly for this purpose, coupled with a NSRunLoop in the daemon.

    But normally they're used in conjunction with NSConnection as part of Cocoa's distributed objects. See the Distributed Objects Programming Topics guide.

Share This Page