gpt4 book ai didi

c++ - 如何使用 boost.asio 解析 ftp 站点?

转载 作者:行者123 更新时间:2023-11-28 03:55:02 24 4
gpt4 key购买 nike

Boost.asio 文档不支持任何 ftp 示例。

 `boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("www.boost.org", "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);`

解析 http 站点并获取 HTTP 端点。

但是 tcp::resolver::query query("ftp://ftp.remotesensing.org/gdal/", "ftp");

导致主机未找到错误。那么如何解析一个ftp站点。

最佳答案

如果您的主机名中仍然包含 ftp://,那么您的主机名应该是“ftp.remotesensing.org”

#include "stdafx.h"

#include <iostream>
#include <boost/asio.hpp>

using namespace boost::asio::ip;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
try
{

boost::asio::io_service io_service;
tcp::resolver resolver(io_service);

tcp::resolver::query query("ftp.remotesensing.org", "ftp");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}

if (!error)
{
cout << "socket connected: " << socket.is_open() << endl;
// Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf constructor.
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");

std::istream response_stream(&response);
std::string sResponse;
while (!response_stream.eof())
{
response_stream >> sResponse;
cout << sResponse << " ";
}
}
else
{
cout << "Error: " << error.message() << endl;
}

}catch(std::exception& e)
{
std::cout << e.what() << std::endl;
}

return 0;
}

关于c++ - 如何使用 boost.asio 解析 ftp 站点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3931805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com