手动创建远程时Cocoa分布式对象异常NSSocketPort

问题描述:

这真的是在做我的头,我希望有人能解决我的问题。手动创建远程时Cocoa分布式对象异常NSSocketPort

我试图学习分布式对象,bonjour等与可可。

我可以让事情正常运行,但只有一个令我讨厌的案例,我不明白为什么会发生。

我试图设置一个DO服务器,宣传自己与Bonjour。

下面是相关的服务器代码:

- (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:]超时

这是为什么?

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

我希望有人能阐明一些道理。我在这个过程中所做的所有搜索都显示了一些类似的人,但我找不到解决我的案例的答案,或者告诉我为什么会发生这种情况。

谢谢!

编辑:只是为了澄清...我试图这样做的原因是我只是好奇,如果它可以完成而不使用NSSocketPortNameServer。

引用apple.com,你读到最后一行:

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

对NSSocket API不了解很多(即没有),但它可能是你永远不会发送一个字节,因此等待什么都没有发生,并超时。

+0

这并不能真正帮助。这是一个鸡和鸡蛋的情况下,如果是这种情况 – Ben 2010-07-20 23:06:21

+0

但如果客户端应该建立连接,不应该至少..连接?就这样扔一个字节? – mvds 2010-07-20 23:17:56

+0

你可以tcpdump看看发生了什么? – mvds 2010-07-20 23:19:43

MVDS,这里有两个msgSends输出的DIFF结果..

差异msgSends-NOTWORKING 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: 
+0

我认为在你的版本中,端口不会被添加到runloop。请参阅'> - NSPort NSPort addConnection:toRunLoop:forMode:'。还有很多'NSPortMessage'的东西缺少差异的一侧。由于我看到异常情况,你可能没有在'try'部分之前做一个钝的'exit()'。它会让事情变得更加清晰,但我认为您已经可以轻松地找到完整跟踪中的差异。在'initRemoteWithTCPPort'(可以在两条轨迹中找到)看看第一件看起来像'NSRunLoop'或'NSPortMessage'的东西 – mvds 2010-07-22 02:35:34

+0

btw:'egrep'^> [^ _] + $''得到有趣的东西。 – mvds 2010-07-22 02:40:18

+0

再次感谢您的帮助。该应用程序运行在可可应用程序中,因此可能会在diff中存在一些额外的“噪音”。调用addConnection:toRunLoop:forMode: 没有改变任何东西。根据文档,NSConnection无论如何都会调用这个方法。 hmmmm ... – Ben 2010-07-22 04:31:27