0

这真的让我很头疼,我希望有人能解决我的问题。

我正在尝试使用 Cocoa 学习分布式对象、bonjour 等。

我可以让事情正常运行,但只有一个案例让我很恼火,我不明白为什么会这样。

我正在尝试设置一个用 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)正常。我在服务上调用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 被初始化,我们都很高兴。

但是如果我这样做 - “手动”创建一个远程 TCPPort 而不是使用 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);
 }
}

然后对connection.rootProxy的调用总是抛出异常:“ [NSPortCoder sendBeforeTime:sendReplyPort:] timed out

这是为什么?

我可以在两个不同的端口对象上执行的所有日志记录显示它们之间没有差异,但一项工作,一项没有。

我希望有人能解释一下。我在这个网站上所做的所有搜索都向人们展示了一些类似的东西,但我找不到解决我问题的答案,或者告诉我为什么会这样。

谢谢!

编辑:只是为了澄清......我尝试这样做的原因是我很好奇它是否可以在不使用 NSSocketPortNameServer 的情况下完成。

4

2 回答 2

0

引用apple.com,您是否阅读了最后一行:

initRemoteWithTCPPort:host:
将接收器初始化为 SOCK_STREAM 类型的 TCP/IP 套接字,它可以连接到指定端口上的远程主机。
...
在发送数据之前,不会打开与远程主机的连接。

对 NSSocket api 了解不多(即一无所知),但可能是您从不发送一个字节,因此等待什么都没有发生,然后超时。

于 2010-07-20T23:00:34.277 回答
0

mvds,这是两个 msgSends 输出的差异结果..

差异 msgSends-不工作 msgSends-工作

9a10
> + AppController NSObject instanceMethodSignatureForSelector:
573a575
> + NSData NSData dataWithBytesNoCopy:length:freeWhenDone:
640a643
> + NSDistantObject NSDistantObject newDistantObjectWithCoder:
770,777d772
< + NSException NSException exceptionWithName:reason:userInfo:
< + NSException NSException raise:format:
< + NSException NSException raise:format:arguments:
< + NSException NSObject alloc
< + NSException NSObject allocWithZone:
< + NSException NSObject initialize
< + NSException NSObject resolveInstanceMethod:
< + NSException NSObject self
1076a1072,1073
> + NSMachPort NSObject class
> + NSMachPort NSObject initialize
1143a1141
> + NSMessageBuilder NSMessageBuilder initialize
1273d1270
< + NSNumber NSNumber numberWithInt:
1287a1285
> + NSObject NSObject instanceMethodSignatureForSelector:
1435a1434,1435
> + NSPortMessage NSObject allocWithZone:
> + NSPortMessage NSObject initialize
1749d1748
< + NSTextInputContext NSTextInputContext _applicationDeactivated:
1808,1809d1806
< + NSThread NSThread callStackReturnAddresses
< + NSThread NSThread callStackSymbols
2010,2014d2006
< + _NSCallStackArray NSArray allocWithZone:
< + _NSCallStackArray NSObject alloc
< + _NSCallStackArray NSObject initialize
< + _NSCallStackArray NSObject new
< + _NSCallStackArray _NSCallStackArray arrayWithFrames:count:symbols:
2374d2365
< - AppController NSObject isEqual:
2607d2597
< - NSApplication NSApplication _setMouseActivationInProgress:
3245d3234
< - NSCFNumber NSCFNumber intValue
3812,3813d3800
< - NSComboBox NSView _drawRectIfEmpty
< - NSComboBox NSView _drawRectIfEmptyWhenSubviewsCoverDirtyRect:
4094d4080
< - NSComboBoxWindow NSWindow _deactivateTrackingRectsForApplicationDeactivation
4687a4674,4675
> - NSConcreteData NSData isEqual:
> - NSConcreteData NSData isEqualToData:
4755a4744
> - NSConcreteMutableData NSData isNSData__
4794a4784,4785
> - NSConcretePortCoder NSCoder allowsKeyedCoding
> - NSConcretePortCoder NSCoder decodeObject
4796a4788,4792
> - NSConcretePortCoder NSConcretePortCoder decodeBytesWithReturnedLength:
> - NSConcretePortCoder NSConcretePortCoder decodeRetainedObject
> - NSConcretePortCoder NSConcretePortCoder decodeReturnValue:
> - NSConcretePortCoder NSConcretePortCoder decodeValueOfObjCType:at:
> - NSConcretePortCoder NSConcretePortCoder dispatch
4800a4797
> - NSConcretePortCoder NSConcretePortCoder importObject:
4803a4801
> - NSConcretePortCoder NSConcretePortCoder versionForClassName:
4805a4804,4805
> - NSConcretePortCoder NSObject retain
> - NSConcretePortCoder NSObject zone
4830d4829
< - NSConcreteTextStorage NSObject autorelease
4849a4849
> - NSConnection NSConnection addClassNamed:version:
4850a4851
> - NSConnection NSConnection decodeReleasedProxies:
4851a4853,4854
> - NSConnection NSConnection handlePortCoder:
> - NSConnection NSConnection handleReleasedProxies:length:
4861a4865
> - NSConnection NSConnection sendReleasedProxies
4863a4868
> - NSConnection NSConnection versionForClassNamed:
4898a4904,4905
> - NSCountedSet NSCountedSet addObject:
> - NSCountedSet NSCountedSet countForObject:
4900a4908,4909
> - NSCountedSet NSCountedSet member:
> - NSCountedSet NSCountedSet removeObject:
4930a4940
> - NSDOStreamData NSDOStreamData bytes
4932a4943,4944
> - NSDOStreamData NSDOStreamData length
> - NSDOStreamData NSData isNSData__
4933a4946
> - NSDOStreamData NSObject retain
4949a4963
> - NSDistantObject NSDistantObject description
4953a4968,4969
> - NSDistantObject NSDistantObject retain
> - NSDistantObject NSDistantObject retainWireCount
4955a4972
> - NSDistantObject NSProxy respondsToSelector:
5098d5114
< - NSEvent NSEvent data2
5106a5123
> - NSEvent NSEvent trackingNumber
5116,5123d5132
< - NSException NSException dealloc
< - NSException NSException description
< - NSException NSException initWithName:reason:userInfo:
< - NSException NSObject autorelease
< - NSException NSObject class
< - NSException NSObject isKindOfClass:
< - NSException NSObject release
< - NSException NSObject respondsToSelector:
5251d5259
< - NSHelpManager NSHelpManager _orderOutHelpWindow
5338a5347
> - NSInvocation NSInvocation _addAttachedObject:
5340a5350
> - NSInvocation NSInvocation getReturnValue:
5342a5353,5355
> - NSInvocation NSInvocation setArgument:atIndex:
> - NSInvocation NSInvocation setSelector:
> - NSInvocation NSInvocation setTarget:
5346a5360
> - NSInvocation NSObject retain
5532d5545
< - NSLayoutManager NSLayoutManager _hasSeenRightToLeft
5559d5571
< - NSLayoutManager NSLayoutManager characterIndexForPoint:inTextContainer:fractionOfDistanceBetweenInsertionPoints:
5570d5581
< - NSLayoutManager NSLayoutManager fractionOfDistanceThroughGlyphForPoint:inTextContainer:
5575,5576d5585
< - NSLayoutManager NSLayoutManager glyphIndexForPoint:inTextContainer:
< - NSLayoutManager NSLayoutManager glyphIndexForPoint:inTextContainer:fractionOfDistanceThroughGlyph:
5578d5586
< - NSLayoutManager NSLayoutManager glyphRangeForBoundingRectWithoutAdditionalLayout:inTextContainer:
5744a5753
> - NSLevelIndicatorCell NSObject isEqual:
5939a5949
> - NSMethodSignature NSMethodSignature methodReturnType
6268a6279
> - NSObjectController NSObject isEqual:
6365d6375
< - NSPlaceholderNumber NSPlaceholderNumber initWithInt:
6379a6390,6391
> - NSPort NSObject dealloc
> - NSPort NSPort addConnection:toRunLoop:forMode:
6380a6393,6399
> - NSPortMessage NSObject release
> - NSPortMessage NSPortMessage components
> - NSPortMessage NSPortMessage dealloc
> - NSPortMessage NSPortMessage initWithSendPort:receivePort:components:
> - NSPortMessage NSPortMessage receivePort
> - NSPortMessage NSPortMessage sendPort
> - NSPortMessage NSPortMessage setMsgid:
6447a6467,6471
> - NSRunLoop NSRunLoop _addPort:forMode:
> - NSRunLoop NSRunLoop _containsPort:forMode:
> - NSRunLoop NSRunLoop _enumerateInfoPairsWithBlock:
> - NSRunLoop NSRunLoop _removePort:forMode:
> - NSRunLoop NSRunLoop addPort:forMode:
6448a6473
> - NSRunLoop NSRunLoop containsPort:forMode:
6450a6476
> - NSRunLoop NSRunLoop removePort:forMode:
6823a6850
> - NSSocketPort NSObject hash
6824a6852
> - NSSocketPort NSPort removeConnection:fromRunLoop:forMode:
6825a6854,6855
> - NSSocketPort NSSocketPort _handleMessage:from:socket:
> - NSSocketPort NSSocketPort _incrementUseCount
6828a6859
> - NSSocketPort NSSocketPort addConnection:toRunLoop:forMode:
6830a6862
> - NSSocketPort NSSocketPort handlePortMessage:
6836a6869
> - NSSocketPort NSSocketPort removeFromRunLoop:forMode:
6837a6871
> - NSSocketPort NSSocketPort scheduleInRunLoop:forMode:
6838a6873
> - NSSocketPort NSSocketPort setDelegate:
7035a7071
> - NSTempAttributeDictionary NSAttributeDictionary count
7334d7369
< - NSTextInputContext NSTextInputContext deactivate
7361,7362d7395
< - NSTextView NSTextView _cellForPoint:characterIndex:level:row:column:range:
< - NSTextView NSTextView _characterRangeBetweenIndexes:
7378,7379d7410
< - NSTextView NSTextView _getGlyphIndex:characterIndex:forWindowPoint:pinnedPoint:anchorPoint:useAnchorPoint:preferredTextView:partialFraction:
< - NSTextView NSTextView _getGlyphIndex:characterIndex:forWindowPoint:pinnedPoint:preferredTextView:partialFraction:
7388d7418
< - NSTextView NSTextView _selectedRanges
7391d7420
< - NSTextView NSTextView _setScrollingToEnd:
7405,7406d7433
< - NSTextView NSTextView becomeKeyWindow
< - NSTextView NSTextView becomeMainWindow
7441d7467
< - NSTextView NSTextView mouseDown:
7446d7471
< - NSTextView NSTextView resignKeyWindow
7450,7451d7474
< - NSTextView NSTextView selectionAffinity
< - NSTextView NSTextView selectionRangeForProposedRange:granularity:
7474d7496
< - NSTextView NSTextView setSelectionGranularity:
7523d7544
< - NSTextView NSView _disableTrackingArea:
7528d7548
< - NSTextView NSView _enableTrackingArea:
7552d7571
< - NSTextView NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:
7554d7572
< - NSTextView NSView _recursiveSetTrackingAreasDirty:
7573d7590
< - NSTextView NSView _windowChangedKeyState
7662d7678
< - NSThemeFrame NSObject isEqual:
7808,7809d7823
< - NSThemeFrame NSView _drawRectIfEmpty
< - NSThemeFrame NSView _drawRectIfEmptyWhenSubviewsCoverDirtyRect:
8042d8055
< - NSView NSResponder acceptsFirstResponder
8068,8069d8080
< - NSView NSView _drawRectIfEmpty
< - NSView NSView _drawRectIfEmptyWhenSubviewsCoverDirtyRect:
8130d8140
< - NSView NSView acceptsFirstMouse:
8159d8168
< - NSView NSView needsPanelToBecomeKey
8181d8189
< - NSView NSView shouldDelayWindowOrderingForEvent:
8259d8266
< - NSWindow NSWindow _deactivateTrackingRectsForApplicationDeactivation
8266d8272
< - NSWindow NSWindow _disableTrackingArea:
8280d8285
< - NSWindow NSWindow _enableTrackingArea:
8283d8287
< - NSWindow NSWindow _getPositionFromServer
8342d8345
< - NSWindow NSWindow _removeMouseMovedListener:
8398d8400
< - NSWindow NSWindow _updateMouseMovedState
8481d8482
< - NSWindow NSWindow nextEventMatchingMask:
8492,8493d8492
< - NSWindow NSWindow resignKeyWindow
< - NSWindow NSWindow resignMainWindow
8602,8606d8600
< - _NSCallStackArray NSObject autorelease
< - _NSCallStackArray NSObject init
< - _NSCallStackArray NSObject release
< - _NSCallStackArray NSObject retain
< - _NSCallStackArray _NSCallStackArray dealloc
8816d8809
< - _NSKeyboardFocusClipView NSView _enableOrDisableTrackingAreas
8843d8835
< - _NSKeyboardFocusClipView NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:
8867d8858
< - _NSKeyboardFocusClipView NSView _windowChangedKeyState
8882d8872
< - _NSKeyboardFocusClipView NSView hitTest:
于 2010-07-22T01:39:34.597 回答