20

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?

chester89
  • 8,328
  • 17
  • 68
  • 113

4 Answers4

16

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.

VolkA
  • 34,983
  • 7
  • 37
  • 37
  • 1
    tcp::resolver::query query("localhost", "daytime"); //it works //I wanted to test the example on localhost – chester89 Feb 15 '09 at 17:56
9

read old manual!

ip::tcp::resolver resolver(my_io_service);
ip::tcp::resolver::query query("www.boost.org", "http");
ip::tcp::resolver::iterator iter = resolver.resolve(query);
ip::tcp::resolver::iterator end; // End marker.
while (iter != end)
{
    ip::tcp::endpoint endpoint = *iter++;
    std::cout << endpoint << std::endl;
}

http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/overview/networking/protocols.html

sergeyxzc
  • 515
  • 6
  • 4
4

I had the same problem right now (with the same tutorial). Change the server code to:

tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 19876));

and change the client code:

tcp::resolver::query query(argv[1], "19876");

to make the same service work on a different port.

ducin
  • 25,621
  • 41
  • 157
  • 256
0

If I not mistake, you are trying to use UNICODE string -- tchar. Use standard

int main(int argc,char **argv)
Artyom
  • 31,019
  • 21
  • 127
  • 215