My Thread Programs Block
- by user315378
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;
}