【问题标题】:How to check FTP connectivity using CURL library in c?如何在 c 中使用 CURL 库检查 FTP 连接?
【发布时间】:2014-08-04 09:15:54
【问题描述】:

我想在 c 程序中使用 curl 库检查 FTP 服务器的连接性。谁能告诉我如何在不使用任何数据传输的情况下做到这一点,这意味着我不想传输任何文件来检查它。我想要的就像 CURLOPT_CONNECT_ONLY 选项,它仅适用于 HTTP、SMTP 和 POP3 协议,不适用于 FTP。

卷曲版本:7.24 要求:FTP服务器连接测试。

【问题讨论】:

    标签: c curl ftp libcurl connectivity


    【解决方案1】:

    在下面的例子中,只有连接请求将被传递到 FTP 服务器,如果服务器可以 ping,那么它将给出 CURLE_OK 返回代码,否则在特定超时(60 秒)后给出失败响应。其他选项您可以根据您的要求从http://curl.haxx.se/libcurl/c/ 设置。

    ...
    snprintf(ftp_url, BUF_LEN_512, "ftp://%s:%s@%s", uploadConf->username, uploadConf->password, uploadConf->ip);
    
    // Reset curl lib
    curl_easy_reset(curl);
    
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, throw_away);
    if (CURLE_OK != (res = curl_easy_setopt(curl, CURLOPT_URL, ftp_url)))
    {
        printf("Failed to check ftp url, Error : %s : %d\n", curl_easy_strerror(res), res);
    }
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
    
    // Connection establishment timeout
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 60);
    
    if (CURLE_OK != (res = curl_easy_perform(curl)))
    {
        /* If fail to connect */
    }
    else
    {
        /* If connected succesfully */
    }
    
    
    static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
    {
        size_t res;
    
        res = (size_t)(size * nmemb);
    
        /* we are not interested in the headers itself, so we only return the size we would have saved ... */
        return res;
    }
    

    希望它能帮助大家在 c 中使用 libcurl 测试与 FTP 服务器的连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      相关资源
      最近更新 更多