Hi,
This is really doing my head in, I hope someone can solve my issue.
I'm trying to learn distributed object, bonjour, etc with Cocoa.
I can get things up and running but there's just one case that's annoying me, i don't understand why it's happening.
I'm trying to setup a DO server that advertise itself with Bonjour.
Here's the relevant server code:
- (void) startServer
{
NSSocketPort *socket = [[NSSocketPort alloc] init];
pubService = [[NSNetService alloc] initWithDomain:@"local." type:@"_myservice._tcp" name:@"my_server" port:[socket socket]];
[pubService publish];
theConnection = [[NSConnection alloc] initWithReceivePort:socket sendPort:nil];
[theConnection setRootObject:self];
[[NSSocketPortNameServer sharedInstance] registerPort:socket name:@"my_server"];
[theConnection setDelegate:self];
[theConnection retain];
}
For the client, i'll skip some code sample, I use a NSNetServiceBrowser and search for the appropriate services. It finds the service (NSNetService) OK. I call resolveWithTimeout on the service, and that works well too.
When the service is resolved I try to connect to it.
if I connect like this:
- (void) connect:(NSNetService *) service;
{
NSLog(@"Remote is %@ [%@] [%d]",[service hostName],[service name],[service port]);
NSSocketPort *port = (NSSocketPort *) [[NSSocketPortNameServer sharedInstance] portForName:[service name] host:[service hostName]];
connection = [[NSConnection connectionWithReceivePort:nil sendPort:port] retain];
@try
{
clientObject = (NSDistantObject<DOProtocol>*) connection.rootProxy;
}
@catch (id exception) {
NSLog(@"Caught exception %@",exception);
}
}
then everything works well, clientObject get initialised and we're all happy.
But if I do this - "manually" creating a remote TCPPort instead of using NSSocketPortNameServer:
- (void) connect:(NSNetService *) service;
{
NSLog(@"Remote is %@ [%@] [%d]",[service hostName],[service name],[service port]);
NSSocketPort *port = [[NSSocketPort alloc] initRemoteWithTCPPort:[service port] host:[service hostName]];
connection = [[NSConnection connectionWithReceivePort:nil sendPort:port] retain];
@try
{
clientObject = (NSDistantObject<DOProtocol>*) connection.rootProxy;
}
@catch (id exception) {
NSLog(@"Caught exception %@",exception);
}
}
Then the call to connection.rootProxy always throws the exception: "[NSPortCoder sendBeforeTime:sendReplyPort:] timed out"
Why is that?
All the logging that I can do on the two different port object show no differences between them, yet one work, one doesn't.
I hope somebody can shed some light. All the search I did on this show people with some similar but I can't find an answer that solve my case, or tells me why it's happening.
Thanks!
EDIT: just to clarify... the reason i'm trying this is I'm just curious if it can be done without using the NSSocketPortNameServer.