我正在启动Boost.Asio并尝试在官方网站上做出示例.
这里的客户代码:
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; }
问题是 - 我无法找出从命令提示符运行程序的参数是什么?
您将使用要连接的服务器的IP或主机名运行该程序.tcp :: resolver :: query使主机解析或IP作为第一个参数和服务名称(如在Unix主机上的/ etc/services中定义) - 您也可以使用数字服务标识符(也就是端口)数).它返回可能的端点列表,因为单个主机可能有多个条目.
阅读旧手册!
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