Can`t really understand what the parameters for constructing tcp::resolver::query

chester89 picture chester89 · Feb 15, 2009 · Viewed 18.4k times · Source

I am starting Boost.Asio and trying to make examples given on official website work.
here`s client code:


using boost::asio::ip::tcp;

int _tmain(int argc, _TCHAR* argv[])
{
    try {
        boost::asio::io_service io_service;

        tcp::resolver resolver(io_service);
        tcp::resolver::query query(argv[1], "daytime");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;

        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)
            throw boost::system::system_error(error);

        for(;;) {
            boost::array buf;
            boost::system::error_code error;

            std::size_t len = socket.read_some(boost::asio::buffer(buf), error);

            if (error == boost::asio::error::eof)
                break; //connection closed cleanly by peer
            else if (error)
                throw boost::system::system_error(error);

            std::cout.write(buf.data(), len);
        }
    }
    catch(std::exception& e) {
        //...
    }
    return 0;
}

The question is - I can not find out what the parameters would be to run program from command prompt?

Answer

VolkA picture VolkA · Feb 15, 2009

You would run the program with the IP or Hostname of the server you want to connect to. tcp::resolver::query takes the host to resolve or the IP as the first parameter and the name of the service (as defined e.g. in /etc/services on Unix hosts) - you can also use a numeric service identifier (aka port number). It returns a list of possible endpoints, as there might be several entries for a single host.