2010-04-13 4 views
0

서버로 작동하는 프로그램을 작성했습니다. "accept"가 프로그램을 차단하고 있음을 알았습니다. 이 문장을 사용하여 프로그램을 차단하지 못하도록 막을 내리고 싶었지만 여전히 발생합니다. 아무도 도와 줄 수 있습니까? 우편 번호 감사내 스레드 프로그램 블록

-(IBAction)Connetti{ 

    if(switchConnessione.on){ 

     int port = [fieldPort.text intValue]; 

     labelStatus.text = [[NSString alloc] initWithFormat:@"Il Server è attivo"]; 

     server_len = sizeof(server); 

     server.sin_family = AF_INET; 
     server.sin_port = htons((u_short)port); 
     server.sin_addr.s_addr = INADDR_ANY; 

     sd = socket (AF_INET, SOCK_STREAM, 0); 

     bind(sd, (struct sockaddr*)&server, sizeof(server)); 

     listen(sd, 1); 

     [NSThread detachNewThreadSelector:@selector(startThreadAccept) toTarget:self withObject:nil]; 

     } 

    else { 
     labelStatus.text = [[NSString alloc] initWithFormat:@"Server non attivo"]; 

     switchChat.on = FALSE; 
     switchChat.enabled = FALSE; 
    } 

    } 

-(void)startThreadAccept{ 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 

    [self performSelectorOnMainThread:@selector(acceptConnection) withObject:nil waitUntilDone:NO]; 
    [pool release]; 

    } 

-(void)acceptConnection{ 

    new_sd = accept(sd, (struct sockaddr*)&server, &server_len); 
    labelStatus.text = [[NSString alloc] initWithFormat:@"Ho accettato una connessione:%d", new_sd]; 
    switchChat.enabled = TRUE; 



} 
+0

크래시 또는 차단합니까? 질문의 제목과 본문이 일치하지 않습니다. –

답변

0

당신은 여전히 ​​주 스레드에서 accept()를 호출합니다. 다른 스레드에서 연결을 허용하려면 -performSelectorOnMainThread: 호출을 제거해야합니다.

+0

이것이 좋은 해결책일까요? (아래) – zp26

0

이 내 새로운 방법

입니다 - (IBAction를) Connetti {

//code 
[NSThread detachNewThreadSelector:@selector(acceptConnection) toTarget:self withObject:nil]; 
//code 
} 

- (무효) acceptConnection {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 

new_sd = accept(sd, (struct sockaddr*)&server, &server_len); 
labelStatus.text = [[NSString alloc] initWithFormat:@"Ho accettato una connessione:%d", new_sd]; 
switchChat.enabled = TRUE; 
[pool release]; 

} 

그것의 올바른 해결책? 왜 어떤 경우에는 스레드가 시작되지 않는 것 같습니까? 감사합니다

+0

그게 더 낫지 만, 각 accept는 아마도 문자열을 누설합니다 (labelStatus가 객체이고 텍스트가 해당 객체의 retain 또는 copy 속성이라고 가정). 당신은 아마 원하는 [NSString stringWithFormat : ....] – JeremyP

관련 문제