0

我有一个适用于 iOS 的 stunnel 版本,它运行良好,但是当我尝试重新启动它时,应用程序崩溃了。

我从 pthread_create 开始。这是重新启动的代码。

int ret;
ret = pthread_cancel(old_threadID);
if (ret != 0)
    NSLog(@"%i", ret);

void *res;
ret = pthread_join(old_threadID, &res);
if (ret != 0)
    NSLog(@"%i", ret);

if (res == PTHREAD_CANCELED)
    NSLog(@"main(): thread was canceled\n"); //This returns OK
else
    NSLog(@"main(): thread wasn't canceled (shouldn't happen!)\n");

//Begin new thread
pthread_t threadID;
//launch stunnel in a thread
int errorStunnel = pthread_create(&threadID, NULL, &stunnel_routine, (void *)fname);
NSLog(@"Error code!! : %i", errorStunnel); //error is 0.

此应用程序崩溃后。它崩溃是因为无法正常启动。正如我在 stunnel.log 中看到的那样:“错误绑定:443 到 127.0.0.1:12345 绑定:地址已在使用(48)”配置文件没有改变。那么为什么端口仍然与先前的线程绑定?

4

1 回答 1

0

取消线程时是否关闭了套接字?套接字文件描述符由整个进程共享。如果您只是删除线程,则套接字仍然打开并绑定到该端口,因为网络层不知道也不关心您正在运行多少线程。它只知道该进程仍然打开了 fd。

于 2013-11-06T02:02:24.567 回答