while(1) 阻止我的接收线程

发布于 2024-08-29 08:51:20 字数 778 浏览 6 评论 0原文

我对这段代码有疑问。 正如您所看到的,使用内部线程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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

节枝 2024-09-05 08:51:20

您不应该使用 while( 1 ) 循环,因为它会阻止您的线程接收信息。
您应该查看 NSRunLoop 类。

[编辑]
根据要求,这里是一个使用运行循环的基本 Obj-C 程序的示例。 :)

#import <Cocoa/Cocoa.h>

BOOL loopShoudRun = YES;

int main( void )
{
    NSAutoreleasePool * pool;
    NSRunLoop         * loop;

    pool = [ [ NSAutoreleasePool alloc ] init ];
    loop = [ NSRunLoop currentRunLoop ];

    while( loopShoudRun == YES && [ loop runMode: NSDefaultRunLoopMode beforeDate: [ NSDate distantFuture ] ] );

    [ pool release ]
    return 0;
}

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. : )

#import <Cocoa/Cocoa.h>

BOOL loopShoudRun = YES;

int main( void )
{
    NSAutoreleasePool * pool;
    NSRunLoop         * loop;

    pool = [ [ NSAutoreleasePool alloc ] init ];
    loop = [ NSRunLoop currentRunLoop ];

    while( loopShoudRun == YES && [ loop runMode: NSDefaultRunLoopMode beforeDate: [ NSDate distantFuture ] ] );

    [ pool release ]
    return 0;
}
人生百味 2024-09-05 08:51:20
labelRicevuti.text = [[NSString alloc] initWithFormat:@"In attesa di ricevere i dati"];

除非 labelRicevuti 是普通的 C 结构,否则此行会泄漏您分配的字符串。

char datiRicevuti[500];
int ricevuti;

//while(1){
ricevuti = recv(new_sd, &datiRicevuti, 500, 0);

你永远不会检查 Ricevuti 这里是否为-1。

labelRicevuti.text = [[NSString alloc] initWithFormat:@"%s", datiRicevuti];

该行可能存在 seg 错误,因为 datiRicevuti 几乎可以肯定不是以 null 结尾的字符序列。首先,您在使用缓冲区之前从未将其清零。其次,如果有 500 或更多可用字节,则允许 recv 完全填满它,因此没有空间用于终止 nul。

此外,您还会泄漏分配的字符串,除非 labelRicevuti 是普通的 C 结构,在这种情况下,您会泄漏在上一次迭代中分配的字符串或函数顶部的字符串。

labelRicevuti.text = [[NSString alloc] initWithFormat:@"In attesa di ricevere i dati"];

This line leaks the string you have allocated unless labelRicevuti is an ordinary C struct.

char datiRicevuti[500];
int ricevuti;

//while(1){
ricevuti = recv(new_sd, &datiRicevuti, 500, 0);

You never check ricevuti to see if it is -1 here.

labelRicevuti.text = [[NSString alloc] initWithFormat:@"%s", datiRicevuti];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文