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

时间:2010-10-14 09:28:54

标签: c++ boost ftp boost-asio

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");

导致主机找不到ERROR。那么如何解决ftp网站。

1 个答案:

答案 0 :(得分:2)

如果您的主机名中仍有ftp://,AFAIR您的主机名应为“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;
}