gpt4 book ai didi

objective-c - 使用分布式对象时出现 NSPort TimeoutException

转载 作者:行者123 更新时间:2023-12-03 16:47:53 26 4
gpt4 key购买 nike

我正在使用分布式对象进行进程间通信。我正在运行一台服务器和四个客户端实例,但我收到 NSPortTimeoutException。我想存储客户端对象@server以供客户端之间进一步通信。
//客户端代码

int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

ClientCode *client = [[ClientCode alloc] init];

/* Get the proxy */
id <myProtocol> proxy = (id <myProtocol>)[NSConnection rootProxyForConnectionWithRegisteredName: @"DirectoryServer" host:nil usingNameServer: [NSSocketPortNameServer sharedInstance]];

if ([proxy conformsToProtocol:@protocol(myProtocol)]) {
NSLog(@"YES");
}
else {
NSLog(@"NO");
}

NSString *recDataFromServer = [proxy print: client];
NSLog(@"%@", recDataFromServer);
/* Release the pool */
[pool release]
return 0;
}

@protocol myProtocol
-(NSString *)print : (id)client;
@end
@interface ClientCode : NSObject {


}
@end
@implementation ClientCode

-(void)startClient
{
NSLog(@"server called me");
}
@end

服务器代码

@interface printHello : NSObject <myProtocol>{
NSMutableArray *saveClassObject;
}
@end

@implementation printHello


- (id) init
{
self = [super init];
if (self != nil) {
saveClassObject = [[NSMutableArray alloc] init];
}
return self;
}

-(NSString *)print : (bycopy id)client
{
[saveClassObject addObject: client];
//NSLog(@"%@", NSStringFromClass([client class]));

NSLog(@"client calling.....");
[client startClient];
NSLog(@"%@", saveClassObject);
return @"parag";
}


-(void)recieveMessage
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread detachNewThreadSelector:@selector(startObserver) toTarget:self withObject:nil];
[pool release];
}

-(void)startObserver
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

printHello *telephoneDirectory= [[printHello alloc] init];

/*
* Create a new socket port for your connection.
*/
NSSocketPort *port = [NSSocketPort port];

NSLog(@"%d", [port isValid]);
/*
* Create a connection using the socket port.
*/
NSConnection *connXion = [NSConnection connectionWithReceivePort: port
sendPort: port];
/*
* Set the responding server object as
* the root object for this connection.
*/
[connXion setRootObject: telephoneDirectory];

/*
* Try to register a name for the NSConnection,
* and report an error if this is not possible.
*/
if ([connXion registerName: @"DirectoryServer"
withNameServer: [NSSocketPortNameServer sharedInstance]] == NO)
{
NSLog(@"Unable to register as 'DirectoryServer'");
NSLog(@"Perhaps another copy of this program is running?");
exit(1);
}

/* Start the current runloop. */
[[NSRunLoop currentRunLoop] run];

[telephoneDirectory release];
/* Release the pool */
[pool release];
return 0;

}

输出@client

2011-10-11 20:50:35.415 client[54606:903] called
2011-10-11 20:50:35.424 client[54606:903] (null)
2011-10-11 20:50:35.420 client[54608:903] called
2011-10-11 20:50:35.425 client[54608:903] (null)
2011-10-11 20:50:35.424 client[54607:903] called
2011-10-11 20:50:35.428 client[54607:903] (null)
2011-10-11 20:50:35.434 client[54609:903] called
2011-10-11 20:50:35.437 client[54609:903] (null)
2011-10-11 20:50:36.091 client[54606:903] YES
2011-10-11 20:50:36.093 client[54609:903] YES
2011-10-11 20:50:36.093 client[54607:903] YES
2011-10-11 20:50:36.094 client[54608:903] YES
2011-10-11 20:50:36.098 client[54606:903] server called me
2011-10-11 20:50:36.103 client[54608:903] server called me
2011-10-11 20:50:36.117 client[54608:903] parag
2011-10-11 20:50:36.119 client[54609:903] server called me
2011-10-11 20:50:36.125 client[54607:903] server called me
2011-10-11 20:50:36.127 client[54606:903] parag
2011-10-11 20:50:36.125 client[54609:903] *** Terminating app due to uncaught exception 'NSPortTimeoutException', reason: '[NOTE: this exception originated in the server.]
[NSPortCoder sendBeforeTime:sendReplyPort:] timed out (10340039236.124821 340039236.124922) 1'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff825487b4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff84d95f03 objc_exception_throw + 45
2 Foundation 0x00007fff86416483 -[NSConnection sendInvocation:internal:] + 4304
3 CoreFoundation 0x00007fff8251a98c ___forwarding___ + 860
4 CoreFoundation 0x00007fff82516a68 _CF_forwarding_prep_0 + 232
5 client 0x0000000100000d4f main + 307
6 client 0x0000000100000c14 start + 52
7 ??? 0x0000000000000001 0x0 + 1
)

输出@server

2011-10-11 20:50:36.095 DistributedServer[54579:1303] client calling.....
2011-10-11 20:50:36.098 DistributedServer[54579:1303] client calling.....
2011-10-11 20:50:36.100 DistributedServer[54579:1303] client calling.....
2011-10-11 20:50:36.102 DistributedServer[54579:1303] client calling.....
2011-10-11 20:50:36.115 DistributedServer[54579:1303] (
"<ClientCode: 0x100111250>",
"<ClientCode: 0x100111250>",
"<ClientCode: 0x100111250>",
"<ClientCode: 0x100111250>"
)

当我第二次运行客户端时,我在客户端控制台获得输出,但在服务器控制台什么也没有得到。

客户端第二次输出

2011-10-11 20:53:54.514 client[54801:903] called
2011-10-11 20:53:54.518 client[54801:903] (null)
2011-10-11 20:53:54.521 client[54800:903] called
2011-10-11 20:53:54.526 client[54800:903] (null)
2011-10-11 20:53:54.516 client[54804:903] called
2011-10-11 20:53:54.526 client[54804:903] (null)
2011-10-11 20:53:54.529 client[54803:903] called
2011-10-11 20:53:54.533 client[54803:903] (null)
2011-10-11 20:53:54.955 client[54803:903] YES
2011-10-11 20:53:54.956 client[54800:903] YES
2011-10-11 20:53:54.956 client[54801:903] YES
2011-10-11 20:53:54.956 client[54804:903] YES
2011-10-11 20:53:54.961 client[54804:903] server called me
2011-10-11 20:53:54.963 client[54801:903] server called me
2011-10-11 20:53:54.964 client[54801:903] parag
2011-10-11 20:53:54.965 client[54800:903] server called me
2011-10-11 20:53:54.966 client[54800:903] parag
2011-10-11 20:53:54.966 client[54804:903] parag
2011-10-11 20:53:54.969 client[54803:903] server called me
2011-10-11 20:53:54.970 client[54803:903] parag

关于如何修复 NSPortTimeoutException 有什么想法吗?

最佳答案

NSPortTimeoutException 出现是因为我正在保存客户端 @server 的对象,并且在客户端终止后没有删除该对象。现在我的代码工作正常。

关于objective-c - 使用分布式对象时出现 NSPort TimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7728564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com