My Thread Programs Block

Posted by user315378 on Stack Overflow See other posts from Stack Overflow or by user315378
Published on 2010-04-13T11:01:00Z Indexed on 2010/04/13 11:02 UTC
Read the original article Hit count: 352

Filed under:
|

I wrote a program that worked as a server. Knowing that "accept" was blocking the program. I wanted to launch a thread with this statement to prevent precisely that the program crashes, but this still happens. Can anybody help? Post code Thanks

-(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;

}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about threads