これは本当に頭を悩ませています。誰かが私の問題を解決してくれることを願っています。
Cocoaで分散オブジェクトやbonjourなどを学ぼうとしています。
私は物事を立ち上げて実行することができますが、私を悩ませているケースが1つだけあります.なぜそれが起こっているのかわかりません.
Bonjour でアドバタイズする DO サーバーをセットアップしようとしています。
関連するサーバーコードは次のとおりです。
- (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];
}
クライアントについては、いくつかのコード サンプルをスキップします。NSNetServiceBrowser を使用して、適切なサービスを検索します。サービス (NSNetService) が OK であることがわかります。サービスで resolveWithTimeout を呼び出しますが、これもうまく機能します。
サービスが解決されたら、それに接続しようとします。
このように接続すると:
- (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);
}
}
その後、すべてがうまく機能し、clientObject が初期化され、私たちは満足しています。
しかし、これを行うと、 NSSocketPortNameServer を使用する代わりにリモート TCPPort を「手動で」作成します。
- (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);
}
}
次に、connection.rootProxy への呼び出しは常に例外をスローします: " [NSPortCoder sendBeforeTime:sendReplyPort:] timed out "
何故ですか?
2 つの異なるポート オブジェクトで実行できるすべてのログは、それらの間に違いはありませんが、一方は機能しますが、他方は機能しません。
誰かが光を当てることができることを願っています。このショーで行ったすべての検索では、似たような人がいますが、私のケースを解決する答えが見つからないか、なぜそれが起こっているのかを教えてくれます.
ありがとう!
編集:明確にするために...私がこれを試している理由は、NSSocketPortNameServerを使用せずに実行できるかどうかだけです。