我的线程程序块 [英] My Thread Programs Block

查看:62
本文介绍了我的线程程序块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个用作服务器的程序. 知道接受"正在阻止程序. 我想使用此语句启动线程,以防止程序被阻止,但这仍然会发生. 有人可以帮忙吗? 邮政编码 谢谢

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 blocked, 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;



}

推荐答案

您仍然在主线程上调用accept().如果要在其他线程上接受连接,则需要删除-performSelectorOnMainThread:调用.

You still call accept() on the main thread. If you want the connection to be accepted on a different thread, then you need to remove the -performSelectorOnMainThread: call.

这篇关于我的线程程序块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆