2013-02-08 59 views
1

我不能管理鬻代理對象,它只是停留在客戶端時,試圖獲取連接進程之間的通信採用分佈式對象

NSConnection *conn = [NSConnection connectionWithRegisteredName:@"server" host:nil]; 

這裏是我註冊的連接和鬻對象

//server 
-(void)initServer { 

NSConnection * connection = [[NSConnection alloc]init]; 

MZRemoteObject * obj = [[MZRemoteObject alloc]init]; 
obj.message = @"hello world"; 
[connection setRootObject:obj]; 
if([connection registerName:@"server"]== NO){ 
    NSLog(@"error Register Server"); 
} 
NSLog(@"Registered Server"); 
} 

客戶端(獲得販賣的對象)

- (void)recieveMessage { 

NSDistantObject *proxy; 
NSConnection *conn = [NSConnection connectionWithRegisteredName:@"server" host:nil]; 

if (!conn) { 
    NSLog(@"conn recieve message error"); 
} 

proxy = [conn rootProxy]; 
MZRemoteObject * obj =(MZRemoteObject*)proxy; 

if (!proxy) { 
    NSLog(@"proxy,recieve message error"); 
} 
NSLog(@"----%@",obj.message); 
} 

可以在任何一個告訴我,我是什麼做錯了?

+0

你是從線程調用initServer嗎? –

+0

我從主線程調用兩種方法,但是從不同的應用程序客戶端應用程序和服務器應用程序 – Andrew

+0

您是否使用沙盒?服務器是實際的應用程序,而不是命令行工具嗎?有沒有寫入控制檯日誌?你有沒有考慮過使用'+ serviceConnectionWithName:rootObject:'和'+ rootProxyForConnectionWithRegisteredName:host:'方便的構造函數? –

回答

0
[[NSRunLoop currentRunLoop] run]; 

在 - (void)initServer方法中啓動當前的runloop。

-(void)initServer { 

NSConnection * connection = [[NSConnection alloc]init]; 

MZRemoteObject * obj = [[MZRemoteObject alloc]init]; 
obj.message = @"hello world"; 
[connection setRootObject:obj]; 
if([connection registerName:@"server"]== NO){ 
    NSLog(@"error Register Server"); 
} 
NSLog(@"Registered Server"); 
[[NSRunLoop currentRunLoop] run]; 
} 
+0

有沒有必要在主線程上調用它 – Andrew

+0

對不起,我錯了:) – Andrew