how to resolve ftp site using boost.asio?
Boost.asio documentation doesn't support any ftp examples.
`boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("www.b开发者_高级运维oost.org", "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);`
that resolve http site and get an HTTP endpoint.
but tcp::resolver::query query("ftp://ftp.remotesensing.org/gdal/", "ftp");
that result a Host not found ERROR. So how to resolve a ftp site.
For one you've still got the ftp:// in your host name, AFAIR your host name should be "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;
}
精彩评论