Distributed Objects (newbie)
Hello All
I wonder whether anyone can just give me a hint as to where I'm going adrift with my code.
I am new to Cocoa, learning fast, but just have this wee problem:
I want to vend an object on one Mac and be able to message it from another on the LAN.
In my 'server' app, I use the following code to establish a connection:
-(void)awakeFromNib { NSSocketPort *port = [[NSSocketPort alloc] initWithTCPPort:60000]; NSConnection *connection = [[NSConnection alloc] initWithReceivePort: port sendPort: nil]; RemoteControl *remCon = [[RemoteControl alloc] init]; [connection setRootObject:remCon];
RemoteControl is a custom class in my 'server' app, remCon is an instance of this class, and the
port does open correctly and can be seen to be open on the network.
In my 'client' app, I have the following code:
- (void)awakeFromNib {
NSSocketPort *port = [[NSSocketPort alloc] initRemoteWithTCPPort:60000 host: @"192.168.1.2"];
if(port != NO) {
NSLog(@"Port Setup %@\n\n\n", port);
}
NSConnection *connection = [NSConnection connectionWithReceivePort: nil sendPort: port];
if(connection != NO) {
NSLog(@"Connection Setup %@\n\n\n", connection);
}
NSDistantObject *remObject = [connection rootProxy];
NSLog(@"Connection Setup %@\n\n\n", remObject);
[remObject retain];
[connection retain];
[port retain];
}
- (IBAction)remFill:(id)sender {
[remObject fillField];
}
Whenever I try this out, I get the following error on the log:
2010-04-23 16:08:31.547 client[5717] *** -[NSProxy doesNotRecognizeSelector:fillField] called!
Now, my 'server' object (Controller) DOES respond to the fillField method when it is called
from a button on its own interface, but not when trying the remote call from another Mac.
Am I doing something really stupid ?
Thanks in advance.
Chris
Hi Chris
Your code's about right. I've taken your code and persuaded it to work. I've published the code on:
http://clanmills.com/files/ClientServer.zip
Take a look. Be gentle - it's hot off the Mac. I'll have to go over it a little more and make it a little more robust. I've never looked at the remote stuff in Cocoa before. Very interesting.
I've set up a server "WeeServer". Be sure you have it running before you run the client "WeeClient". When you press "Speak to Server", you'll see that the Server increments his Hit Count.

I've been writing a series of tutorials about Cocoa. If you're happy, I'd like to write this up and publish on Monday.
Incidentally, if you want to run multiple clients, you can start them the terminal with the command:
$ .../ClientServer/WeeClient/build/Debug/WeeClient.app/Contents/MacOS/WeeClient
deep in the bowls of every .app lies a good old command line program. You can run it to start multiple copies of most GUI programs.
Good Luck. Let me know how it goes. We'll get this fixed for you. Cocoa's not easy at first - however it's worth the effort to learn.