我想在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/