久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

libcurl?curl_multi?非阻塞方式?請(qǐng)求網(wǎng)頁(yè)?http

 wtkc 2015-03-03

#include <iostream>
#include <curl/curl.h>

using namespace std;

int main(int argc, char **argv)
{
    // 初始化
    curl_global_init(CURL_GLOBAL_WIN32);
    CURLM *multi_handle = NULL;
    CURL *easy_handle1 = NULL;
    CURL *easy_handle2 = NULL;

    extern size_t save_sina_page(void *buffer, size_t size, size_t count, void *user_p);
    extern size_t save_sohu_page(void *buffer, size_t size, size_t count, void *user_p);
    FILE *fp_sina = fopen("sina.html", "ab+");
    FILE *fp_sohu = fopen("sohu.html", "ab+");

    multi_handle = curl_multi_init();

    // 設(shè)置easy handle
    easy_handle1 = curl_easy_init();
    curl_easy_setopt(easy_handle1, CURLOPT_URL, "http://www.sina.com.cn");
    curl_easy_setopt(easy_handle1, CURLOPT_WRITEFUNCTION, &save_sina_page);
    curl_easy_setopt(easy_handle1, CURLOPT_WRITEDATA, fp_sina);

    easy_handle2 = curl_easy_init();
    curl_easy_setopt(easy_handle2, CURLOPT_URL, "http://www.sohu.com");
    curl_easy_setopt(easy_handle2, CURLOPT_WRITEFUNCTION, &save_sohu_page);
    curl_easy_setopt(easy_handle2, CURLOPT_WRITEDATA, fp_sohu);

    // 添加到multi stack
    curl_multi_add_handle(multi_handle, easy_handle1);
    curl_multi_add_handle(multi_handle, easy_handle2);


    int running_handle_count;
    while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &running_handle_count))
    {
        cout << running_handle_count << endl;
    }

    while (running_handle_count)
    {
        timeval tv;
        tv.tv_sec = 1;
        tv.tv_usec = 0;

        int max_fd;
        fd_set fd_read;
        fd_set fd_write;
        fd_set fd_except;

        FD_ZERO(&fd_read);
        FD_ZERO(&fd_write);
        FD_ZERO(&fd_except);

        curl_multi_fdset(multi_handle, &fd_read, &fd_write, &fd_except, &max_fd);
        int return_code = select(max_fd + 1, &fd_read, &fd_write, &fd_except, &tv);
        if (-1 == return_code)
        {
            cerr << "select error." << endl;
            break;
        }
        else
        {
            while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &running_handle_count))
            {
                cout << running_handle_count << endl;
            }
        }
    }

    // 釋放資源
    fclose(fp_sina);
    fclose(fp_sohu);
    curl_easy_cleanup(easy_handle1);
    curl_easy_cleanup(easy_handle2);
    curl_multi_cleanup(multi_handle);
    curl_global_cleanup();

    return 0;
}

size_t save_sina_page(void *buffer, size_t size, size_t count, void *user_p)
{
    return fwrite(buffer, size, count, (FILE *)user_p);
}

size_t save_sohu_page(void *buffer, size_t size, size_t count, void *user_p)
{
    return fwrite(buffer, size, count, (FILE *)user_p);
}


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多