Inter-Process messaging in cocoa

Objective-c supports an inter-process messaging system. An application running on different machines and on same computer can send message’s to each other.

Objective-c classes use for inter-process messaging are as follows:-

NSProxy etc..

NSConnection object manage the communication between objects and normally run in the background.

NSPort object represent the communication channel. Its an abstract class and sending and receiving messages handle through port.

NSProxy is an abstract superclass and typically a proxy forward the message to real object and load the real object.

Example: A program using NSSocketPort, NSConnection to send message from one computer to another computer.

Step 1) Create a cocoa application project using Xcode.

Step 2) Interface file

@interface AppDelegate : NSObject <NSApplicationDelegate>
NSMutableArray*          _messArr;

NSString*                _recPort;
NSString*                _sendPort;

IBOutlet NSTextField*    _txtField;//In which we type text
IBOutlet NSTableView*    _tableView;//Which shows the  sending and receiving messages

@property (assign) IBOutlet NSWindow *window;

– (IBAction) sendBtnAction:(id)sender;//after typing text on click on send button send message to another computer


Step 3) Implementation file

//Initialize mess array to load into table view
– (id) init
if ((self = [super init]))
_messArr = [[NSMutableArray alloc] initWithObjects:@”Start Chat”, nil];
return self;

– (void) applicationDidFinishLaunching:(NSNotification *)aNotification
//Before creating a connection and register a port
Initializing sending and receiving port name
eg: On computer 1
receiving port name is “Computer1”
sending port name is “Computer2”
then on another computer
receiving port name is “Computer2”
sending port name is “Computer1”

//creating connection and register for port to recieve data
NSSocketPort* port = [[NSSocketPort alloc] init];
NSConnection* connection = [NSConnection             connectionWithReceivePort:port sendPort:nil];
[[NSSocketPortNameServer sharedInstance] registerPort:port     name:_recPort];

//Here _recPort is of NSString type and initialize before                        creating      connection

[connection setRootObject:self];
[port release];
[connection runInNewThread];

Step 4) Define send button actions

– (IBAction) sendBtnAction:(id)sender
[self reloadTableView:[_txtField stringValue]];
//Creating connection with sending port
NSSocketPort* port = (NSSocketPort*)    [[NSSocketPortNameServer sharedInstance] portForName:_sendPort host:@”*”];
NSConnection* connection = [NSConnection connectionWithReceivePort:nil sendPort:port];
NSDistantObject* serverObject = nil;
serverObject = [connection rootProxy];
[(AppDelegate*)serverObject reloadTableView:[_txtField stringValue]];
[_txtField setStringValue:@””];//Empty the textfield after sending

Step 5)  Table view data sources method and user define reload method

This method adding new object to mess array and the reloading table view with new data
– (void) reloadTableView:(NSString*)data
[_messArr addObject:data];
[_tableView reloadData];

#pragma Mark – NSTableViewDataSource Methods
– (NSInteger) numberOfRowsInTableView:(NSTableView*)tableView
return [_messArr count];

– (id) tableView:(NSTableView*)tableView objectValueForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row
return [_messArr objectAtIndex:row];

– (void) dealloc
[_messArr release];
[_tableView release];
[_txtField release];
[_recPort release];
[_sendPort release];

[super dealloc];

Written By: Yogesh Arora, Software Engineer, Mindfire Solutions

About yogesha2014

Currently, i am working in MindfireSolution as cocoa application developer. I have 2+ year of experience in mac desktop application development. Also have knowledge about VC++ win32, MFC and MonoDevelop GTK#2.0.

Posted on November 11, 2014, in Cocoa Application, Cooca, Inter-Process Communication (IPC), Objective-C and tagged , , , , , , , , , . Bookmark the permalink. 2 Comments.

Leave a Reply to SutoCom Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: