c++ - thread正确处理socket后抛出错误
迷茫
迷茫 2017-04-17 13:42:36
0
3
514

最近在学习写个简单的http server,流程是来个request就开个线程处理,然后主线程循环accept,但处理request的线程正常运行结束后总是抛出个stat error:no such file or directory。代码如下:

while(true)
{
    int client=accept(listen_sock,(sockaddr*)&remote,&len);
    if(client<0)
        die_err(std::string("accept error").c_str());

    std::thread(process_request,client).detach();
}

void process_request(int client)
{
    std::string method;
    char line[1024];
    std::string firstline;
    std::string path;
    std::string htoc("/Users/imagecmos/untitled");
    if(recv(client,line,sizeof(line),0)<0)
        die_err(std::string("recv error").c_str());
    getline(line, sizeof(line),method,path,firstline);
    htoc+=path;
    struct stat urlpath;
    if(stat(htoc.c_str(),&urlpath)<0)
        die_err(std::string("stat error").c_str());
    if(S_ISDIR(urlpath.st_mode))
    {
        _default(client);
    }
    else if(S_ISREG(urlpath.st_mode))
    {
        normalresponse(client,htoc);
    }
    close(client);
}

奇怪的是我把while(true)去掉,这个错误就没了,请问是什么原因?调了好长时间没看出问题。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全員に返信(3)
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!