c - 如何使用 c 中的 CURL 库检查 FTP 连接?

标签 c curl ftp libcurl connectivity

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

curl 版本:7.24 需求:FTP服务器连通性测试。​​

最佳答案

在下面的示例中,只有连接请求才会传递到 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服务器的连接。

关于c - 如何使用 c 中的 CURL 库检查 FTP 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25115300/

相关文章:

c++ - 点对点连接,当一个点在 NAT 后面时

php - 如何将此命令行 curl 转换为 php curl?

php - 由 php 调用时 curl 未正确执行

javascript - 无法使用 npm 中的 "ftp"包连接到 ftp 服务器

php - 需要基于 Web 的 FTP 帮助

java - 压缩存在于一个 FTP 位置的文件并直接复制到另一个 FTP 位置

c - 在 C 中使用 fread 时,读取 block 大小是否有限制?

c - C中N个进程之间的管道输出

c++ - 在程序中执行一个或多个新进程

bash - 放弃低于最小文件大小的curl 下载