假设我想连接到FTP服务器,然后在活动模式下列出根目录(通过PORT命令)。为此,我将使用cURL库。
似乎cURL提供了这样的功能。有CURLOPT_FTPPORT
选项,官方参考说明,端口可以选择后跟' - '来指定端口范围。
if (CURL* curl = ::curl_easy_init())
{
guard.reset(curl, ::curl_easy_cleanup);
::curl_easy_setopt(curl, CURLOPT_URL, "ftp://10.0.0.162:21");
::curl_easy_setopt(curl, CURLOPT_FTPPORT, "localhost:37000-38000");
CURLcode retval = ::curl_easy_perform(curl);
if (retval != CURLE_OK)
{
throw NetworkError(retval, ::curl_easy_strerror(retval));
}
}
...
但这是一个问题。
如果范围中的第一个端口不可用,curl_easy_perform
将失败并显示CURLE_FTP_PORT_FAILED
。
修改
以详细模式输出:
* Rebuilt URL to: ftp://10.0.0.162:21/
* timeout on name lookup is not supported
* Trying 10.0.0.162...
* Connected to 10.0.0.162 (10.0.0.162) port 21 (#0)
< 220 eugee-server / banner
> USER anonymous
< 331 Anonymous access allowed, send identity (e-mail name) as password.
> PASS ftp@example.com
< 230-Welcome to ftp.eugeeserver
< 230 User logged in.
> PWD
< 257 "/" is current directory.
* Entry path is '/'
* socket failure: Bad access
* Remembering we are in dir ""
* Connection #0 to host 10.0.0.162 left intact