while(1) 阻止我的接收线程
我对这段代码有疑问。 正如您所看到的,使用内部线程recv启动,以便程序被阻塞等待给定但将继续执行,让任务锁定线程。 我的程序将继续接收recv数据套接字new_sd,因此我进入了无限循环(注释的代码)。 问题是,通过在recv之前输入while(1)我的程序块,但没有正确插入它接收一个字符串,但在那之后停止。 有人可以帮助我让我的recv始终等待信息吗? 预先感谢您的帮助。
-(IBAction)聊天{
[NSThread detachNewThreadSelector:@selector(riceviDatiServer) toTarget:self withObject:nil];
}
-(void)riceviDatiServer{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
labelRicevuti.text = [[NSString alloc] initWithFormat:@"In attesa di ricevere i dati"];
char datiRicevuti[500];
int ricevuti;
//while(1){
ricevuti = recv(new_sd, &datiRicevuti, 500, 0);
labelRicevuti.text = [[NSString alloc] initWithFormat:@"%s", datiRicevuti];
//}
[pool release];
}
I have a problem with this code.
As you can see a launch with an internal thread recv so that the program is blocked pending a given but will continue its execution, leaving the task to lock the thread.
My program would continue to receive the recv data socket new_sd and so I entered an infinite loop (the commented code).
The problem is that by entering the while (1) my program block before recv, but not inserting it correctly receives a string, but after that stop.
Someone could help me make my recv always waiting for information?
Thanks in advance for your help.
-(IBAction)Chat{
[NSThread detachNewThreadSelector:@selector(riceviDatiServer) toTarget:self withObject:nil];
}
-(void)riceviDatiServer{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
labelRicevuti.text = [[NSString alloc] initWithFormat:@"In attesa di ricevere i dati"];
char datiRicevuti[500];
int ricevuti;
//while(1){
ricevuti = recv(new_sd, &datiRicevuti, 500, 0);
labelRicevuti.text = [[NSString alloc] initWithFormat:@"%s", datiRicevuti];
//}
[pool release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该使用 while( 1 ) 循环,因为它会阻止您的线程接收信息。
您应该查看 NSRunLoop 类。
[编辑]
根据要求,这里是一个使用运行循环的基本 Obj-C 程序的示例。 :)
You shouldn't use a while( 1 ) loop, as it will prevent your thread to receive information.
You should take a look at the NSRunLoop class.
[EDIT]
As requested, here's an example of a basic Obj-C program that uses a run-loop. : )
除非 labelRicevuti 是普通的 C 结构,否则此行会泄漏您分配的字符串。
你永远不会检查 Ricevuti 这里是否为-1。
该行可能存在 seg 错误,因为 datiRicevuti 几乎可以肯定不是以 null 结尾的字符序列。首先,您在使用缓冲区之前从未将其清零。其次,如果有 500 或更多可用字节,则允许 recv 完全填满它,因此没有空间用于终止 nul。
此外,您还会泄漏分配的字符串,除非 labelRicevuti 是普通的 C 结构,在这种情况下,您会泄漏在上一次迭代中分配的字符串或函数顶部的字符串。
This line leaks the string you have allocated unless labelRicevuti is an ordinary C struct.
You never check ricevuti to see if it is -1 here.
This line probably seg faults, because datiRicevuti is almost certainly not a null terminated sequence of chars. Firstly, you never zeroed out the buffer before using it. Secondly, you allow recv to fill it up completely if it has 500 or more bytes available, so there's no space for a terminating nul.
Also, you leak the allocated string, unless labelRicevuti is an ordinary C struct, in which case you leak the string allocated in the previous iteration or the one at the top of the function.